Skip to main content
ICT
Lesson AB28 - Sets and Maps
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

F. TreeMap page 8 of 11

  1. The java.util.TreeMap class implements the Map interface using a balanced binary search tree ordered by keys. In general, any Comparable objects may be placed into a TreeMap as the key. This guarantees that the keys will be in ascending order, as determined by the key object’s compareTo method.

  2. Because of the balanced binary tree implementation, the TreeMap class provides an O(log n) run time for the operations put, get, and containsKey.

  3. The following code fragment demonstrates implementation of a TreeMap:

    TreeMap studentMap = new TreeMap();

    for (int recNum = 1; recNum <= NUM_STUDENTS; recNum++){
      Student s = new Student();    // declare a new Student
      s.setName(...);               // set the student’s
                                    // attributes
      s.setGradeLevel(...);         // ...
      s.setID(...);                 // ...

      studentMap.put(s.getID(), s); // add student to the map
                                    // using ID as the key
    }
    // Display the student whose key is S964413
    System.out.println(studentMap.get("S964413"));

  4. The Map interface does not specify a method for obtaining an iterator, and the TreeMap class does not have one. Instead, you can get the set of all keys by calling the keySet method, then iterate over that set to get all values. For example:

    TreeMap acronym = new TreeMap();
    String key;
    Definition value; // acronym definition

    acronym.put("CS", new Definition("Computer Science"));
    acronym.put("AP", new Definition("Advanced Placement"));

    Set keys = acronym.keySet();
    Iterator iter = keys.iterator();
    while (iter.hasNext()){
      key = (String)iter.next();
      value = (Definition)acronym.get(key);

      // process value
      System.out.println(key + " stands for " +
                         value.getDefinition());
    }

    The output for this code fragment is

    AP stands for Advanced Placement
    CS stands for Computer Science

    The values will be processed in the ascending order of keys.

  5. TreeMap is more general than TreeSet. Both implement Binary Search Trees, but in TreeSet the values are compared to each other, while in TreeMap, no ordering is assumed for the values and the tree is arranged according to the order of the keys. In a way, a set is a special case of a map where a value serves as its own key.

    import java.util.TreeMap;

       TreeMap <Integer, String> map = new TreeMap <Integer, String> ();
       String name = "John";
       map.put(new Integer(3),name);
       map.put(new Integer(2),"Nancy");
       map.put(new Integer(1),"George");

       System.out.println(“Size of map” + map.size());
       System.out.println(“Person for 1: ” + map.get(1));

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.