TreeMap in Java

TreeMap

In the previous blog, we learned about the Map interface in Java. To learn more about Map interface, visit Map Interface in Java. In this blog, we will learn about a class called TreeMap which implements the Map interface.


TreeMap

TreeMap uses a tree to implement the Map interface. TreeMap is used for storing the elements in key-value pairs in sorted order. TreeMap, unlike HashMap, assures that the elements are sorted in ascending order. It contains only unique elements. TreeMap cannot have null keys but can have multiple null values.


Syntax for Creating TreeMap

TreeMap<Key, Value> numbers = new TreeMap<>();

Methods of TreeMap

  1. put()
  2. put() method is used to insert or add an entry to the Map.

  3. putAll()
  4. The putAll() method inserts or adds the specified map to the map.

    Example of TreeMap using the methods put() and putAll()

    import java.util.*;
    class TreeMapPutMethodsEx {
      public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(101, "John");
        treeMap.put(102, "Smith");
        treeMap.put(103, "Chris");
        treeMap.put(104, "Donna");
        System.out.println("TreeMap1: "+ treeMap);
        TreeMap<Integer, String> treeMap1 = new TreeMap<>();
        treeMap1.put(105, "Charles");
        treeMap1.put(106, "Smith");
        treeMap1.put(107, "Bren");
        treeMap1.put(108, "Bruce");
        System.out.println("TreeMap2: "+ treeMap1);
        treeMap.putAll(treeMap1);
        System.out.println("Combined the elements of TreeMap1 and TreeMap2 using putAll(): "+ map);
      }
    }

    Output

    TreeMap1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    TreeMap2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Combined the elements of TreeMap1 and TreeMap2 using putAll(): {101=John, 102=Smith, 103=Chris, 104=Donna, 105=Charles, 106=Smith, 107=Bren, 108=Bruce}
  5. keySet()
  6. keySet() method returns the collection of keys exist in the map.

  7. values()
  8. values() method returns a set of all the maps of a TreeMap.

  9. get()
  10. get() method returns the object that contains the value associated with the key.

  11. getOrDefault()
  12. getOrDefault() method returns the value to which the specified key is mapped, or if no value is found, it returns defaultValue.

    Example of TreeMap using the methods keySet(), values(), get(), getOrDefault()

    import java.util.*;
    class TreeMapAccessMethodsEx {
      public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(101, "John");
        treeMap.put(102, "Smith");
        treeMap.put(103, "Chris");
        treeMap.put(104, "Donna");
        System.out.println("TreeMap: "+ treeMap);
        System.out.println("Keys elements: "+treeMap.keySet());
        System.out.println("Values elements: "+treeMap.values());
        TreeMap<String, Integer> treeMap1 = new TreeMap<>();
        treeMap1.put("Charles", 105);
        treeMap1.put("Smith", 106);
        treeMap1.put("Bren", 107);
        treeMap1.put("Bruce", 108);
        System.out.println("TreeMap1: "+ treeMap1);
        int key = treeMap1.get("Bruce");
        System.out.println("The value of element is: "+ key);
        int key1 = treeMap1.getOrDefault("Charles", 105);
        System.out.println("The value of element is: "+ key1);
      }
    }

    Output

    TreeMap: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Keys elements: [101, 102, 103, 104]
    Values elements: [John, Smith, Chris, Donna]
    TreeMap1: {Bren=107, Bruce=108, Charles=105, Smith=106}
    The value of element is: 108
    The value of element is: 105
  13. remove(Object k)
  14. remove(Object k) method removes the entry from the map represented by the key k.

    e.g.

    import java.util.*;
    class MapPutAllEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map1: "+ map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map2: "+ map1);
        map.putAll(map1);
        System.out.println("Combined the elements of Map1 and Map2 using putAll(): "+ map);
      }
    }

    Output

    Map1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Map2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Combined the elements of Map1 and Map2 using putAll(): {101=John, 102=Smith, 103=Chris, 104=Donna, 105=Charles, 106=Smith, 107=Bren, 108=Bruce}
  15. firstKey()
  16. firstKey() method is used to return the first key currently in the map.

  17. lastKey()
  18. lastKey() method is used to return the last key currently in the map.

    Example of TreeMap using firstKey() and lastKey()

    import java.util.*;
    class TreeMapFirstKeyLastKeyMethodsEx {
      public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(101, "John");
        treeMap.put(102, "Smith");
        treeMap.put(103, "Chris");
        treeMap.put(104, "Donna");
        System.out.println("TreeMap: "+ treeMap);
        System.out.println("The first key is: "+ treeMap.firstKey());
          System.out.println("The last key is: "+ treeMap.lastKey());
        TreeMap<String, Integer> treeMap1 = new TreeMap<>();
        treeMap1.put("Charles", 105);
        treeMap1.put("Smith", 106);
        treeMap1.put("Bren", 107);
        treeMap1.put("Bruce", 108);
        System.out.println("TreeMap1: "+ treeMap1);
          System.out.println("The first key is: "+ treeMap1.firstKey());
          System.out.println("The last key is: "+ treeMap1.lastKey());
      }
    }

    Output

    TreeMap: {101=John, 102=Smith, 103=Chris, 104=Donna}
    The first key is: 101
    The last key is: 104
    TreeMap1: {Bren=107, Bruce=108, Charles=105, Smith=106}
    The first key is: Bren
    The last key is: Smith
  19. higherKey()
  20. higherKey() method returns the lowest key among those keys that are greater than the specified key.

  21. higherEntry()
  22. higherEntry() method returns the lowest key strictly greater than the given key or returns null if there is no such key.

  23. lowerKey()
  24. lowerKey() method returns the greatest key amongst all the keys that are less than the given key

  25. lowerEntry()
  26. lowerEntry() method returns a key-value mapping associated with the greatest key amongst all keys which are less than the given key.

  27. ceilingKey()
  28. The ceilingKey() method returns the lowest key that is greater than the specified key, or it returns null if no such key is discovered.

  29. ceilingEntry()
  30. The ceilingEntry() method returns the key-value pair with the lowest key that is larger than or equal to the specified key, or it returns null if no such key is discovered.

  31. floorEntry()
  32. floorEntry() method returns the greatest key, less than or equal to the specified key, or returns null if there is no such key found.

    Example of TreeMap using the above methods

    import java.util.*;
    class TreeMapFirstKeyLastKeyMethodsEx {
      public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(101, "John");
        treeMap.put(102, "Smith");
        treeMap.put(103, "Chris");
        treeMap.put(104, "Donna");
        System.out.println("TreeMap: "+ treeMap);
        System.out.println("HigherKey() method: " + treeMap.higherKey(102));
        System.out.println("HigherEntry() method: " + treeMap.higherEntry(104));
        System.out.println("LowerKey() method: " + treeMap.lowerKey(101));
        System.out.println("LowerEntry() method: " + treeMap.lowerEntry(103));
        System.out.println("CeilingKey() method: " + treeMap.ceilingKey(102));
        System.out.println("CeilingEntry() method: " + treeMap.ceilingEntry(101));
        System.out.println("FloorEntry() method: " + treeMap.floorEntry(102));
        TreeMap<String, Integer> treeMap1 = new TreeMap<>();
        treeMap1.put("Charles", 105);
        treeMap1.put("Smith", 106);
        treeMap1.put("Bren", 107);
        treeMap1.put("Bruce", 108);
        System.out.println("TreeMap1: "+ treeMap1);
        System.out.println("HigherKey() method: " + treeMap1.higherKey("Charles"));
        System.out.println("HigherEntry() method: " + treeMap1.higherEntry("Bruce"));
        System.out.println("LowerKey() method: " + treeMap1.lowerKey("Smith"));
        System.out.println("LowerEntry() method: " + treeMap1.lowerEntry("Bruce"));
        System.out.println("CeilingKey() method: " + treeMap1.ceilingKey("Charles"));
        System.out.println("CeilingEntry() method: " + treeMap1.ceilingEntry("Bren"));
        System.out.println("FloorEntry() method: " + treeMap1.floorEntry("Smith"));
      }
    }

    Output

    TreeMap: {101=John, 102=Smith, 103=Chris, 104=Donna}
    HigherKey() method: 103
    HigherEntry() method: null
    LowerKey() method: null
    LowerEntry() method: 102=Smith
    CeilingKey() method: 102
    CeilingEntry() method: 101=John
    FloorEntry() method: 102=Smith
    TreeMap1: {Bren=107, Bruce=108, Charles=105, Smith=106}
    HigherKey() method: Smith
    HigherEntry() method: Charles=105
    LowerKey() method: Charles
    LowerEntry() method: Bren=107
    CeilingKey() method: Charles
    CeilingEntry() method: Bren=107
    FloorEntry() method: Smith=106
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.