# What you can do with a TreeMap : HashTable Map « Collections Data Structure « Java

What you can do with a TreeMap

```

// : c11:SortedMapDemo.java
//What you can do with a TreeMap.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002

import java.util.Iterator;
import java.util.TreeMap;

public class SortedMapDemo {

public static void main(String[] args) {
TreeMap sortedMap = new TreeMap();
sortedMap.put("IBM", "White Plains, NY");
sortedMap.put("Learning Tree", "Los Angeles, CA");
sortedMap.put("Microsoft", "Redmond, WA");
sortedMap.put("Netscape", "Mountain View, CA");
sortedMap.put("O'Reilly", "Sebastopol, CA");
sortedMap.put("Sun", "Mountain View, CA");
System.out.println(sortedMap);
Object low = sortedMap.firstKey(), high = sortedMap.lastKey();
System.out.println(low);
System.out.println(high);
Iterator it = sortedMap.keySet().iterator();
for (int i = 0; i <= 6; i++) {
if (i == 3)
low = it.next();
if (i == 6)
high = it.next();
else
it.next();
}
System.out.println(low);
System.out.println(high);
System.out.println(sortedMap.subMap(low, high));
System.out.println(sortedMap.tailMap(low));
}
} ///:~

```

### Related examples in the same category

 1 Check if a particular key exists in Java Hashtable 2 Check if a particular value exists in Java Hashtable 3 Get Collection of Values from Java Hashtable 4 Get Set view of Keys from Java Hashtable 5 Get Size of Java Hashtable 6 Iterate through keys of Java Hashtable 7 Remove all values from Java Hashtable 8 Scan the content of a hashtable 9 Remove value from Java Hashtable 10 Sort keys in an Hashtable 11 Associates keys with values 12 Iterate through values of Java Hashtable 13 A simple Map implementation 14 Hash table with separate chaining 15 Hash table with linear probing 16 Hash table with double hashing 17 Working with Key-Value Pairs in a Hashtable 18 Demonstrate the Hashtable class, and an Enumeration 19 Demonstrate the HashMap class, and an Iterator 20 Soft HashMap 21 MultiMap extends AbstractMap 22 Array Map extends AbstractMap 23 Demonstrating the WeakHashMap 24 Use treemap 25 Sorting Elements in a TreeMap 26 A Map implemented with ArrayLists 27 Simple demonstration of HashMap 28 HashMap 29 Caching Hashtable 30 Hashtable that supports mostly-concurrent reading, but exclusive writing. 31 Lru Hashtable 32 Bucketized Hashtable 33 Custom hash table based on customized array