Search results

Java: Treemaps

Quick summary

  • Treemaps stores key-value pairs
  • Treemaps guarantees their order
  • It’s common to iterate through a map and see if it has a particular value
  • Treemaps implements a Map interface
  • In Treemaps, keys are sorted

Eclipse example: treemaps

Sample

public class java.util.TreeMap <K, V>
implements SortedMap<K,V>, Cloneable, java.io.Serializable {
public TreeMap();  // constructors
public TreeMap(Comparator<? super K> c);
public TreeMap(Map<? extends K,? extends V> m);
public TreeMap(SortedMap<K,? extends V> m);

public Comparator<? super K> comparator();
public K firstKey();
public K lastKey();
public Object clone();

public SortedMap<K,V> headMap(K toKey);
public SortedMap<K,V> tailMap(K fromKey);
public SortedMap<K,V> subMap(K fromKey, K toKey);
}

There are 4 constructors:

The TreeMap class has four constructors which work in the obvious way. The first one creates an empty TreeMap. The second constructor creates an empty TreeMap that will use the Comparator passed as an argument. The third creates a TreeMap out of the elements in the given Map. The fourth constructor creates a TreeMap out of the elements in the given SortedMap. (Just Java 2)

The method headMap() returns a view of this Map containing only elements that are strictly less than the argument object. The method tailMap() returns a view of elements that are greater than or equal to the argument object. The method subMap() returns a view into the Map that holds only objects starting at the from object inclusive, and going up to, but not including, the to object. (Just Java 2)

Example

import java.util.*;
public class example3  {
// the Map!
Map<String, String> phonebook = new TreeMap<String, String>();

// constructor
public example3(String n[], String nums[]) {
for(int i=0; i< n.length; i++)
phonebook.put( n[i], nums[i] );
}

public static void main(String[] args) {
// data
String [] names = { "Lefty", "Guarav", "Wong", "Rupamay" };
String [] extns = { "4873", "4810", "3769", "0" };

// get an instance of this class
example3 ex = new example3( names, extns );

// dump out the map
System.out.println("map:  " + ex.phonebook);

// get the mappings
Set<Map.Entry<String,String>> s = ex.phonebook.entrySet();

// iterate over the mappings
//   for (Iterator i = s.iterator(); i.hasNext(); ) {
for (Map.Entry me : s) {
Object ok = me.getKey();
Object ov = me.getValue();
System.out.print("key=" + ok );
System.out.println(", value=" + ov );
}
}
}

The output of running this program is now in sorted order because we used TreeMap:

map:  {Guarav=4810, Lefty=4873, Rupamay=0, Wong=3769}
key=Guarav, value=4810
key=Lefty, value=4873
key=Rupamay, value=0
key=Wong, value=3769

Get a specific object in the map

public V get(Object key);

Or you’ll be putting pairs into the table using this method:

public V put(K key, V value);

About Tom Johnson

Tom Johnson

I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, plain language, tech comm careers, and more. Check out my API documentation course if you're looking for more info about documenting APIs. Or see my posts on AI and AI course section for more on the latest in AI and tech comm.

If you're a technical writer and want to keep on top of the latest trends in the tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.