Java Program to Implement TreeMap API

This Java program is to Implement TreeMap API.A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

Here is the source code of the Java program to Implement TreeMap API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.Comparator;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.NavigableMap;
  7. import java.util.NavigableSet;
  8. import java.util.Set;
  9. import java.util.SortedMap;
  10. import java.util.TreeMap;
  11. import java.util.Map.Entry;
  12.  
  13. public class TreeMapImpl<K, V>
  14. {
  15.     private TreeMap<K, V> treeMap;
  16.  
  17.     /** Constructs a new, empty tree map, using the natural ordering of its keys. **/
  18.     public TreeMapImpl()
  19.     {
  20.         treeMap = new TreeMap<K, V>();
  21.     }
  22.  
  23.     /**
  24.      * Constructs a new, empty tree map, ordered according to the given
  25.      * comparator.
  26.      **/
  27.     public TreeMapImpl(Comparator<? super K> comparator)
  28.     {
  29.         treeMap = new TreeMap<K, V>(comparator);
  30.     }
  31.  
  32.     /**
  33.      * Constructs a new tree map containing the same mappings as the given map,
  34.      * ordered according to the natural ordering of its keys.
  35.      **/
  36.     public TreeMapImpl(Map<? extends K, ? extends V> m)
  37.     {
  38.         treeMap = new TreeMap<K, V>(m);
  39.     }
  40.  
  41.     /**
  42.      * Constructs a new tree map containing the same mappings and using the same
  43.      * ordering as the specified sorted map.
  44.      **/
  45.     public TreeMapImpl(SortedMap<K, ? extends V> m)
  46.     {
  47.         treeMap = new TreeMap<K, V>(m);
  48.     }
  49.  
  50.     /**
  51.      * Returns a key-value mapping associated with the least key greater than or
  52.      * equal to the given key, or null if there is no such key.
  53.      **/
  54.     public Map.Entry<K, V> ceilingEntry(K key)
  55.     {
  56.         return treeMap.ceilingEntry(key);
  57.     }
  58.  
  59.     /**
  60.      * Returns the least key greater than or equal to the given key, or null if
  61.      * there is no such key.
  62.      **/
  63.     public K ceilingKey(K key)
  64.     {
  65.         return treeMap.ceilingKey(key);
  66.     }
  67.  
  68.     /** Removes all of the mappings from this map. **/
  69.     public void clear()
  70.     {
  71.         treeMap.clear();
  72.     }
  73.  
  74.     /** Returns a shallow copy of this TreeMap instance. **/
  75.     public Object clone()
  76.     {
  77.         return treeMap.clone();
  78.     }
  79.  
  80.     /**
  81.      * Returns the comparator used to order the keys in this map, or null if
  82.      * this map uses the natural ordering of its keys.
  83.      **/
  84.     public Comparator<? super K> comparator()
  85.     {
  86.         return treeMap.comparator();
  87.     }
  88.  
  89.     /** Returns true if this map contains a mapping for the specified key. **/
  90.     public boolean containsKey(Object key)
  91.     {
  92.         return treeMap.containsKey(key);
  93.     }
  94.  
  95.     /** Returns true if this map maps one or more keys to the specified value. **/
  96.     public boolean containsValue(Object value)
  97.     {
  98.         return treeMap.containsValue(value);
  99.     }
  100.  
  101.     /**
  102.      * Returns a reverse order NavigableSet view of the keys contained in this
  103.      * map.
  104.      **/
  105.     public NavigableSet<K> descendingKeySet()
  106.     {
  107.         return treeMap.descendingKeySet();
  108.     }
  109.  
  110.     /** Returns a reverse order view of the mappings contained in this map. **/
  111.     public NavigableMap<K, V> descendingMap()
  112.     {
  113.         return treeMap.descendingMap();
  114.     }
  115.  
  116.     /** Returns a Set view of the mappings contained in this map. **/
  117.     public Set<Map.Entry<K, V>> entrySet()
  118.     {
  119.         return treeMap.entrySet();
  120.     }
  121.  
  122.     /**
  123.      * Returns a key-value mapping associated with the least key in this map, or
  124.      * null if the map is empty.
  125.      **/
  126.     public Map.Entry<K, V> firstEntry()
  127.     {
  128.         return treeMap.firstEntry();
  129.     }
  130.  
  131.     /** Returns the first (lowest) key currently in this map. **/
  132.     public K firstKey()
  133.     {
  134.         return treeMap.firstKey();
  135.     }
  136.  
  137.     /**
  138.      * Returns the greatest key less than or equal to the given key, or null if
  139.      * there is no such key.
  140.      **/
  141.     public K floorKey(K key)
  142.     {
  143.         return treeMap.floorKey(key);
  144.     }
  145.  
  146.     /**
  147.      * Returns the value to which the specified key is mapped, or null if this
  148.      * map contains no mapping for the key.
  149.      **/
  150.     public V get(Object key)
  151.     {
  152.         return treeMap.get(key);
  153.     }
  154.  
  155.     /**
  156.      * Returns a view of the portion of this map whose keys are strictly less
  157.      * than toKey.
  158.      **/
  159.     public SortedMap<K, V> headMap(K toKey)
  160.     {
  161.         return treeMap.headMap(toKey);
  162.     }
  163.  
  164.     /**
  165.      * Returns a view of the portion of this map whose keys are less than (or
  166.      * equal to, if inclusive is true) toKey.
  167.      **/
  168.     public NavigableMap<K, V> headMap(K toKey, boolean inclusive)
  169.     {
  170.         return treeMap.headMap(toKey, inclusive);
  171.     }
  172.  
  173.     /**
  174.      * Returns a key-value mapping associated with the least key strictly
  175.      * greater than the given key, or null if there is no such key.
  176.      **/
  177.     public Map.Entry<K, V> higherEntry(K key)
  178.     {
  179.         return treeMap.higherEntry(key);
  180.     }
  181.  
  182.     /**
  183.      * Returns the least key strictly greater than the given key, or null if
  184.      * there is no such key.
  185.      **/
  186.     public K higherKey(K key)
  187.     {
  188.         return treeMap.higherKey(key);
  189.     }
  190.  
  191.     /** Returns a Set view of the keys contained in this map. **/
  192.     public Set<K> keySet()
  193.     {
  194.         return treeMap.keySet();
  195.     }
  196.  
  197.     /**
  198.      * Returns a key-value mapping associated with the greatest key in this map,
  199.      * or null if the map is empty.
  200.      **/
  201.     public Map.Entry<K, V> lastEntry()
  202.     {
  203.         return treeMap.lastEntry();
  204.     }
  205.  
  206.     /** Returns the last (highest) key currently in this map. **/
  207.     public K lastKey()
  208.     {
  209.         return treeMap.lastKey();
  210.     }
  211.  
  212.     /**
  213.      * Returns a key-value mapping associated with the greatest key strictly
  214.      * less than the given key, or null if there is no such key.
  215.      **/
  216.     public Map.Entry<K, V> lowerEntry(K key)
  217.     {
  218.         return treeMap.lowerEntry(key);
  219.     }
  220.  
  221.     /**
  222.      * Returns the greatest key strictly less than the given key, or null if
  223.      * there is no such key.
  224.      **/
  225.     public K lowerKey(K key)
  226.     {
  227.         return treeMap.lowerKey(key);
  228.     }
  229.  
  230.     /** Returns a NavigableSet view of the keys contained in this map. **/
  231.     public NavigableSet<K> navigableKeySet()
  232.     {
  233.         return treeMap.navigableKeySet();
  234.     }
  235.  
  236.     /**
  237.      * Removes and returns a key-value mapping associated with the least key in
  238.      * this map, or null if the map is empty.
  239.      **/
  240.     public Map.Entry<K, V> pollFirstEntry()
  241.     {
  242.         return treeMap.pollFirstEntry();
  243.     }
  244.  
  245.     /**
  246.      * Removes and returns a key-value mapping associated with the greatest key
  247.      * in this map, or null if the map is empty.
  248.      **/
  249.     public Map.Entry<K, V> pollLastEntry()
  250.     {
  251.         return treeMap.pollLastEntry();
  252.     }
  253.  
  254.     /** Associates the specified value with the specified key in this map. **/
  255.     public V put(K key, V value)
  256.     {
  257.         return treeMap.put(key, value);
  258.     }
  259.  
  260.     /** Copies all of the mappings from the specified map to this map. **/
  261.     public void putAll(Map<? extends K, ? extends V> map)
  262.     {
  263.         treeMap.putAll(map);
  264.     }
  265.  
  266.     /** Removes the mapping for this key from this TreeMap if present. **/
  267.     public V remove(Object key)
  268.     {
  269.         return treeMap.remove(key);
  270.     }
  271.  
  272.     /** Returns the number of key-value mappings in this map. **/
  273.     public int size()
  274.     {
  275.         return treeMap.size();
  276.     }
  277.  
  278.     /**
  279.      * Returns a view of the portion of this map whose keys range from fromKey
  280.      * to toKey.
  281.      **/
  282.     public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey,	boolean toInclusive)
  283.     {
  284.         return treeMap.subMap(fromKey, fromInclusive, toKey, toInclusive);
  285.     }
  286.  
  287.     /**
  288.      * Returns a view of the portion of this map whose keys range from fromKey,
  289.      * inclusive, to toKey, exclusive.
  290.      **/
  291.     public SortedMap<K, V> subMap(K fromKey, K toKey)
  292.     {
  293.         return treeMap.subMap(fromKey, toKey);
  294.     }
  295.  
  296.     /** Returns a Collection view of the values contained in this map. **/
  297.     public Collection<V> values()
  298.     {
  299.         return treeMap.values();
  300.     }
  301.  
  302.     public static void main(String... arg)
  303.     {
  304.         TreeMapImpl<Integer, Integer> treeMap = new TreeMapImpl<Integer, Integer>();
  305.         treeMap.put(10, 100);
  306.         treeMap.put(89, -89);
  307.         treeMap.put(45, 345);
  308.         treeMap.put(90, 23);
  309.         Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
  310.         anotherMap.put(34, 9);
  311.         anotherMap.put(23, 00);
  312.         treeMap.putAll(anotherMap);
  313.  
  314.         System.out.println("the key set of the treeMap map is ");
  315.         Set<Integer> keySet = treeMap.keySet();
  316.         Iterator<Integer> itr = keySet.iterator();
  317.         while (itr.hasNext())
  318.         {
  319.             System.out.print(itr.next() + "\t");
  320.         }
  321.         System.out.println();
  322.  
  323.         System.out.println("the values of the treeMap is ");
  324.         Collection<Integer> collectionValues = treeMap.values();
  325.         itr = collectionValues.iterator();
  326.         while (itr.hasNext())
  327.         {
  328.             System.out.print(itr.next() + "\t");
  329.         }
  330.         System.out.println();
  331.  
  332.         System.out.println("poll first entry of the map ");
  333.         Map.Entry<Integer, Integer> pollFirstEntry = treeMap.pollFirstEntry();
  334.         System.out.println("key = " + pollFirstEntry.getKey() + " value = " + pollFirstEntry.getValue());
  335.  
  336.         System.out.println("poll last entry of the map ");
  337.         Map.Entry<Integer, Integer> pollLastEntry = treeMap.pollLastEntry();
  338.         System.out.println("key = " + pollLastEntry.getKey() + " value = " + pollLastEntry.getValue());
  339.  
  340.         System.out.println("the entry set of the treeMap is ");
  341.         Iterator<Entry<Integer, Integer>> eitr;
  342.         Set<Entry<Integer, Integer>> entrySet = treeMap.entrySet();
  343.         eitr = entrySet.iterator();
  344.         while (eitr.hasNext())
  345.         {
  346.             System.out.println(eitr.next() + "\t");
  347.         }
  348.         System.out.println("the treeMap contains Key 34 :" + treeMap.containsKey(34));
  349.         System.out.println("the  treeMap contains Value 600 :" + treeMap.containsValue(600));
  350.  
  351.         System.out.println("the size of the treeMap is " + treeMap.size());
  352.         treeMap.clear();
  353.     }
  354. }

$ javac  TreeMapImpl.java
$ java TreeMapImpl
the key set of the treeMap map is 
10	23	34	45	89	90	
the values of the treeMap is 
100	0	9	345	-89	23	
poll first entry of the map 
key = 10 value = 100
poll last entry of the map 
key = 90 value = 23
the entry set of the treeMap is 
23=0	
34=9	
45=345	
89=-89	
the treeMap contains Key 34 :true
the  treeMap contains Value 600 :false
the size of the treeMap is 4

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.