NavigableSet and NavigableMap Interface in Java

NavigableSet and NavigableMap Interface in Java |  Java 1.6 Version Enhancement in Collection Framework | As part of the 1.6 version the following 2 concepts were introduced in the collection framework:-

  • NavigableSet(I)
  • NavigableMap(I)
  • Collection(I) 1.2v
    • Set(I) 1.2v
      • SortedSet(I) 1.2v
        • NavigableSet(I) 1.6v
          • TreeSet 1.2v

NavigableSet is the child interface of SortedSet and it defines several methods for navigation purposes.

NavigableSet defines the following methods:-

  • floor(e):- It returns the highest element which is <= e.
  • lower(e):- It returns highest element which is < e.
  • ceiling(e):- It returns lowest elements is which is >= e.
  • higher(e):- It returns lowest element which is > e.
  • pollFirst():- It removes and returns the first element.
  • pollLast():- It removes and returns the last element.
  • descendingSet():- It returns NavigableSet in reverse order.
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet = new TreeSet<>();
        treeSet.add(1000);
        treeSet.add(1500);
        treeSet.add(2000);
        treeSet.add(2500);
        treeSet.add(3000);
        treeSet.add(4000);
        System.out.println(treeSet);
        // [1000, 1500, 2000, 2500, 3000, 4000]

        System.out.println(treeSet.ceiling(2000)); // 2000
        System.out.println(treeSet.higher(2000)); // 2500
        System.out.println(treeSet.floor(3000)); // 3000
        System.out.println(treeSet.lower(3000)); // // 2500
        System.out.println(treeSet.pollFirst()); // 1000
        System.out.println(treeSet.pollLast()); // 4000

        System.out.println(treeSet); // [1500, 2000, 2500, 3000]
        System.out.println(treeSet.descendingSet()); // [3000, 2500, 2000, 1500]
    }
}
  • Map(I) 1.2v
    • SortedMap(I) 1.2v
      • NavigableMap(I) 1.6v
        • TreeMap 1.2v

NavigableMap is the child interface of SortedMap. It defines several methods for navigation purposes.

NavigableMap defines the following methods:-

  • floorKey(e)
  • lowerKey(e)
  • ceilingKey(e)
  • higherKey(e)
  • pollFirstEntry()
  • pollLastEntry()
  • descendingMap()
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("B", "Banana");
        treeMap.put("C", "Cat");
        treeMap.put("A", "Apple");
        treeMap.put("D", "Dog");
        treeMap.put("G", "Good");
        System.out.println(treeMap);
        // {A=Apple, B=Banana, C=Cat, D=Dog, G=Good}

        System.out.println(treeMap.ceilingKey("C")); // C
        System.out.println(treeMap.higherKey("E")); // G
        System.out.println(treeMap.floorKey("E")); // D
        System.out.println(treeMap.lowerKey("E")); // D
        System.out.println(treeMap.pollFirstEntry()); // A=Apple
        System.out.println(treeMap.pollLastEntry()); // G=Good

        System.out.println(treeMap); // {B=Banana, C=Cat, D=Dog}
        System.out.println(treeMap.descendingMap()); // {D=Dog, C=Cat, B=Banana}
    }
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *