KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > TreeMap

java.util
Class TreeMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.TreeMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>, SortedMap<K,V>
See Also:
Top Examples, Source Code, HashMap, Hashtable, Comparable, Comparator, Collection, Collections.synchronizedMap(Map)

public void clear()
See Also:
V, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[205]Create a structure like a HashMap with case insensitive keys
By Anonymous on 2005/07/08 08:49:16  Rate
//the key in HashMap is case sensitive 
 //Here is how do you create a structure like a HashMap with case insensitive keys  
  
  
 import java.util.*; 
  
  
 class ComparatorTest implements Comparator  {  
   public int compare ( Object o1, Object o2 )   {  
     String s1 =  ( String ) o1; 
     String s2 =  ( String ) o2; 
     return s1.toUpperCase (  ) .compareTo ( s2.toUpperCase (  )  ) ; 
    }                                      
   public boolean equals ( Object o )   {  
     String s =  ( String ) o; 
     return compare ( this, o ) ==0; 
    }  
  }  
  
  
 import java.util.*; 
  
  
 public class tm 
  {  
   public static void main ( String [  ]  args )  
    {  
     TreeMap tm = new TreeMap ( new ComparatorTest (  )  ) ; 
     tm.put ( "soda", "coke" ) ; 
     tm.put ( "Juice", "orange" ) ; 
     tm.put ( "SODA", "7-up" ) ; 
     Set set = tm.entrySet (  ) ; 
     Iterator iterator = set.iterator (  ) ; 
     while (  iterator.hasNext (  )   )   {  
       Map.Entry entry =  ( Map.Entry ) iterator.next (  ) ; 
       System.out.println ( entry.getKey (  )  + "/" + entry.getValue (  )  ) ; 
      }  
    }  
  }  
  
  
 //clear 
 


public Object clone()
See Also:
Cloneable, V, AbstractMap
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Comparator<? super K> comparator()
See Also:
SortedMap
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1573]Sort a TreeMap by its values not its keys
By Anonymous on 2005/10/15 19:45:12  Rate
//Sort a TreeMap by its values not its keys 
  
  
 1. Create a TreeSet of key/value pairs where the Comparator sorts by  
    the *value*  ( the Double )  in each pair.  
 2. Get the key/value pairs from the original TreeMap by calling  
    TreeMap.entrySet (  )   
 3. Add all the pairs from 2 into the TreeSet in 1.  
  
  
  
 import java.util.Comparator;  
 import java.util.Iterator;  
 import java.util.Map;  
 import java.util.Set;  
 import java.util.TreeMap;  
 import java.util.TreeSet;  
  
  
  
 public class SortedValueTest  
  {   
     public static void main (  String [  ]  args  )   
      {   
         Map map = new TreeMap (  ) ;  
         map.put (  "1/2", new Double (  0.5  )   ) ;  
         map.put (  "1/4", new Double (  0.25  )   ) ;  
         map.put (  "1/8", new Double (  0.125  )   ) ;  
         map.put (  "1/16", new Double (  0.0625  )   ) ;  
  
  
  
         Set set = new TreeSet (   
             new Comparator (  )   
              {   
                 public int compare (  Object o1, Object o2  )   
                  {   
                     Map.Entry e1 =  ( Map.Entry ) o1;  
                     Map.Entry e2 =  ( Map.Entry ) o2;  
  
  
  
                     Double d1 =  ( Double ) e1.getValue (  ) ;  
                     Double d2 =  ( Double ) e2.getValue (  ) ;  
  
  
  
                     return d1.compareTo (  d2  ) ;  
                  }   
              }   ) ;  
  
  
  
         set.addAll (  map.entrySet (  )   ) ;  
  
  
  
         for  (  Iterator iter = set.iterator (  ) ; iter.hasNext (  ) ;  )   
          {   
             Map.Entry entry =  ( Map.Entry ) iter.next (  ) ;  
             System.out.println (  entry.getKey (  )  + " = " + entry.getValue (  )   ) ;  
          }   
      }   
  
  
  
  
  }   
  
  
  
 This will output the following:  
  
  
     1/16 = 0.0625  
     1/8 = 0.125  
     1/4 = 0.25  
     1/2 = 0.5  
  
  
  
 


public boolean containsKey(Object key)
See Also:
NullPointerException, ClassCastException, V, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean containsValue(Object value)
See Also:
V, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Set<Map.Entry<K,V>> entrySet()
See Also:
AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public K firstKey()
See Also:
NoSuchElementException, SortedMap
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public V get(Object key)
See Also:
containsKey(Object), NullPointerException, ClassCastException, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SortedMap headMap(Object toKey)
See Also:
NullPointerException, IllegalArgumentException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SortedMap<K,V> headMap(K toKey)
See Also:
NullPointerException, IllegalArgumentException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Set<K> keySet()
See Also:
AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[229]Sorted Map
By Doug Borter dfborter { at } yahoo { dot } com on 2004/04/23 10:32:38  Rate
import java.util.*; 
  
  
 public class tSortedMap  {  
  
  
    public static void main ( String [  ]  args )   {  
       Map m = new TreeMap (  ) ; 
       StringBuffer sb = null; 
  
  
       m.put ( "Barb",new Integer ( 101 )  ) ; 
       m.put ( "Zeke",new Integer ( 102 )  ) ; 
       m.put ( "Mary",new Integer ( 110 )  ) ; 
       m.put ( "Andy",new Integer ( 124 )  ) ; 
       m.put ( "Pete",new Integer ( 9111130 )  ) ; 
  
  
       for  ( Iterator i = m.keySet (  ) .iterator (  ) ; i.hasNext (  )  ; )   {  
          sb = new StringBuffer (  ( String ) i.next (  )  ) ; 
          System.out.println ( sb + " " + m.get ( sb.toString (  )  )  ) ; 
        }  
     }  
  } 


public K lastKey()
See Also:
NoSuchElementException, SortedMap
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object put(Object key,
                  Object value)
See Also:
NullPointerException, ClassCastException, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public V put(K key,
             V value)
See Also:
NullPointerException, ClassCastException, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void putAll(Map<? extends K,? extends V> map)
See Also:
NullPointerException, ClassCastException, AbstractMap
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public V remove(Object key)
See Also:
NullPointerException, ClassCastException, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int size()
See Also:
V, AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SortedMap subMap(Object fromKey,
                        Object toKey)
See Also:
NullPointerException, IllegalArgumentException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SortedMap<K,V> subMap(K fromKey,
                             K toKey)
See Also:
NullPointerException, IllegalArgumentException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SortedMap tailMap(Object fromKey)
See Also:
NullPointerException, IllegalArgumentException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SortedMap<K,V> tailMap(K fromKey)
See Also:
NullPointerException, IllegalArgumentException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public TreeMap()
See Also:
Comparable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public TreeMap(Comparator<? super K> c)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public TreeMap(Map<? extends K,? extends V> m)
See Also:
NullPointerException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1792]HashMap to TreeMap
By Anonymous on 2006/07/25 07:47:21  Rate
The popular implementations of Map are java.util.HashMap and java.util.TreeMap. The HashMap facilitates easy insertion, deletion, and retrieval of elements. On the other hand, TreeMap is a neat alternative to traverse the keys of a map in a sorted order. 
  
  
 import java.util.*; 
  
  
 /** Demonstrates the use of popular java.util.Map implementations. */ 
 public class MapDemo  {  
  
  
   /** The hash map to add data. */ 
   private HashMap hashMap; 
   /** The tree map to sort data. */ 
   private TreeMap treeMap; 
  
  
   /** Starts the demo. */ 
   public void startDemo (  )   {  
  
  
     // Initialize the hash map 
     hashMap = new HashMap (  ) ; 
     hashMap.put ( ???Key2???, ???2??? ) ; 
     hashMap.put ( ???Key4???, ???4??? ) ; 
     hashMap.put ( ???Key1???, ???1??? ) ; 
     hashMap.put ( ???Key3???, ???3??? ) ; 
  
  
     // Sort the hash map using a tree map 
     treeMap = new TreeMap ( hashMap ) ; 
  
  
    }  
  
  
   /** Main. */ 
   public static void main ( String [  ]  args )   {  
  
  
     MapDemo md = new MapDemo (  ) ; 
     md.startDemo (  ) ; 
  
  
     // Print maps 
     System.out.println ( ???HashMap: ??? + md.hashMap ) ; 
     System.out.println ( ???TreeMap: ??? + md.treeMap ) ; 
  
  
    }  
  
  
  }  
  
  
 Following is the output, 
   
 HashMap:  { Key4=4, Key3=3, Key2=2, Key1=1 }  
 TreeMap:  { Key1=1, Key2=2, Key3=3, Key4=4 } 


public TreeMap(SortedMap<K,? extends V> m)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Collection<V> values()
See Also:
AbstractMap, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags