NavigableMap Interface in Java

NavigableMap

In the previous blog, we learned about the SortedMap interface. If you want to learn more about it, visit, visit SortedMap Interface in Java. In this blog, we will go through another interface called the NavigableMap interface.


NavigableMap Interface

NavigableMap interface can be accessed and traversed in ascending or descending order. It is an extension of SortedMap which means it supports the methods of SortedMap along with some new methods. NavigableMap is present in java.util package.


Syntax of NavigableMap

public interface NavigableMap<Key,Value> extends SortedMap<Key,Value>

Creating a NavigableMap

As we have already seen in the previous blogs that both SortedMap and NavigableMap are interfaces that cannot be used as a class, so we need a class TreeMap that implements them.

NavigableMap<Key, Value> navMap = new TreeMap<>();

Methods of NavigableMap

  1. ceilingEntry()
  2. ceilingEntry() method returns the key value mapping associated with the lowest key which is greater than or equal to the given key or returns null if there is no such key.

    e.g.

    import java.util.*;
    public class NavigableMapCeilingEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of ceilingEntry() is: "+ navMap.ceilingEntry(1003));
        System.out.println("The result of ceilingEntry() is: "+ navMap.ceilingEntry(1004));
      }
    }

    Output

    The result of ceilingEntry() is: 1003=Joe
    The result of ceilingEntry() is: 1004=Justin
  3. ceilingKey()
  4. ceilingKey() returns the lowest key that is greater than or equal to the specified key, or null if no such key exists.

    e.g.

    import java.util.*;
    public class NavigableMapCeilingKeyEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of ceilingKey() is: "+ navMap.ceilingKey(1003));
        System.out.println("The result of ceilingKey() is: "+ navMap.ceilingKey(1005));
      }
    }

    Output

    The result of ceilingKey() is: 1003
    The result of ceilingKey() is: null
  5. descendingKeySet()
  6. descendingKeySet() method returns the reverse order NavigableSet view for the given keys that are contained in the map.

    e.g.

    import java.util.*;
    public class NavigableMapDescendingKeySetEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of descendingKeySet() is: "+ navMap.descendingKeySet());
      }
    }

    Output

    The result of descendingKeySet() is: [1004, 1003, 1002, 1001]
  7. descendingMap()
  8. The descendingMap() method returns the map's mapping in reverse order.

    e.g.

    import java.util.*;
    public class NavigableMapDescendingMapEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of descendingMap() is: "+ navMap.descendingMap());
      }
    }

    Output

    The result of descendingMap() is: {1004=Justin, 1003=Joe, 1002=Smith, 1001=John}
  9. firstEntry()
  10. The firstEntry() method returns the entry of the map with the lowest key, or null if the map is empty.

    e.g.

    import java.util.*;
    public class NavigableMapFirstEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of firstEntry() is: "+ navMap.firstEntry());
      }
    }

    Output

    The result of firstEntry() is: 1001=John
  11. floorEntry()
  12. If no such key is discovered, the floorEntry() method delivers the entry with the greatest ky that is less than or equal to the specified key, or it returns null.

    e.g.

    import java.util.*;
    public class NavigableMapFloorEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of floorEntry() is: "+ navMap.floorEntry(1003));
      }
    }

    Output

    The result of floorEntry() is: 1003=Joe
  13. higherEntry()
  14. higherEntry() method returns the entry with the lowest key amongst those keys which are greater than the given key or returns null if there is no such key found.

    e.g.

    public class NavigableMapHigherEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of higherEntry() is: "+ navMap.higherEntry(1003));
      }
    }

    Output

    The result of higherEntry() is: 1004=Justin
  15. higherKey()
  16. higherKey() method returns the lowest key which is greater than the given key or returns null if there is no such key found.

    e.g.

    import java.util.*;
    public class NavigableMapHigherKeyEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1003, "Joe");
        navMap.put(1004, "Justin");
        System.out.println("The result of higherKey() is: "+ navMap.higherKey(1001));
      }
    }

    Output

    The result of higherKey() is: 1002
  17. lastEntry()
  18. The lastEntry() method returns the map entry with the highest key, or null if the map is empty.

    e.g.

    import java.util.*;
    public class NavigableMapLastEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1004, "Joe");
        navMap.put(1003, "Justin");
        System.out.println("The result of lastEntry() is: "+ navMap.lastEntry());
      }
    }

    Output

    The result of lastEntry() is: 1004=Joe
  19. lowerEntry()
  20. lowerEntry() method returns the entry with the highest key that is less than the specified key or returns null if the map is empty.

    e.g.

    import java.util.*;
    public class NavigableMapLowerEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1004, "Joe");
        navMap.put(1003, "Justin");
        System.out.println("The result of lowerEntry() is: "+ navMap.lowerEntry(1004));
        }
    }

    Output

    The result of lowerEntry() is: 1003=Justin
  21. lowerKey()
  22. lowerKey() method returns the highest key which is less than the given key or returns null if the is no such key found.

    e.g.

    import java.util.*;
    public class NavigableMapLowerKeyEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1004, "Joe");
        navMap.put(1003, "Justin");
        System.out.println("The result of lowerKey() is: "+ navMap.lowerKey(1004));
      }
    }

    Output

    The result of lowerKey() is: 1003
  23. pollFirstEntry()
  24. pollFirstEntry() returns and deletes the map's first entry, or returns null if the map is empty.

    e.g.

    import java.util.*;
    public class NavigableMapPollFirstEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1004, "Joe");
        navMap.put(1003, "Justin");
        System.out.println("The result of pollFirstEntry() is: "+ navMap.pollFirstEntry());
      }
    }

    Output

    The result of pollFirstEntry() is: 1001=John
  25. pollLastEntry()
  26. pollLastEntry() returns and remove the last entry of the map or returns null if the map is empty

    e.g.

    import java.util.*;
    public class NavigableMapPollLastEntryEx {
      public static void main(String[] args) {
        NavigableMap<Integer, String> navMap = new TreeMap<>();
        navMap.put(1001, "John");
        navMap.put(1002, "Smith");
        navMap.put(1004, "Joe");
        navMap.put(1003, "Justin");
        System.out.println("The result of pollLastEntry() is: "+ navMap.pollLastEntry());
     	}
    }

    Output

    The result of pollLastEntry() is: 1004=Joe
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.