Java NavigableMap Interface

Introduction

The NavigableMap interface in Java is a part of the java.util package. It extends the SortedMap interface to provide navigation methods that return the closest matches for given search targets. It is commonly implemented by classes such as TreeMap.

Table of Contents

  1. What is the NavigableMap Interface?
  2. Common Methods
  3. Examples of Using the NavigableMap Interface
  4. Conclusion

1. What is the NavigableMap Interface?

The NavigableMap interface extends the SortedMap interface and provides methods to navigate the map and retrieve entries based on closest matches. It allows for greater flexibility in searching and navigating through the map compared to a regular SortedMap.

2. Common Methods

  • lowerEntry(K key): Returns a key-value mapping associated with the greatest key strictly less than the given key.
  • floorEntry(K key): Returns a key-value mapping associated with the greatest key less than or equal to the given key.
  • ceilingEntry(K key): Returns a key-value mapping associated with the smallest key greater than or equal to the given key.
  • higherEntry(K key): Returns a key-value mapping associated with the smallest key strictly greater than the given key.
  • pollFirstEntry(): Removes and returns a key-value mapping associated with the least key.
  • pollLastEntry(): Removes and returns a key-value mapping associated with the greatest key.
  • descendingMap(): Returns a reverse order view of the map.
  • navigableKeySet(): Returns a NavigableSet view of the keys contained in this map.
  • subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive): Returns a view of the portion of this map whose keys range from fromKey to toKey.

3. Examples of Using the NavigableMap Interface

Example 1: Basic Usage of NavigableMap

This example demonstrates how to use a NavigableMap to perform basic operations.

import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapExample {
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, "Three");

        System.out.println("Map: " + map);
        System.out.println("Lower Entry (2): " + map.lowerEntry(2));
        System.out.println("Floor Entry (2): " + map.floorEntry(2));
        System.out.println("Ceiling Entry (2): " + map.ceilingEntry(2));
        System.out.println("Higher Entry (2): " + map.higherEntry(2));
    }
}

Output:

Map: {1=One, 2=Two, 3=Three}
Lower Entry (2): 1=One
Floor Entry (2): 2=Two
Ceiling Entry (2): 2=Two
Higher Entry (2): 3=Three

Example 2: Polling Entries

This example shows how to use pollFirstEntry and pollLastEntry methods.

import java.util.NavigableMap;
import java.util.TreeMap;

public class PollingEntriesExample {
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, "Three");

        System.out.println("Initial Map: " + map);
        System.out.println("Poll First Entry: " + map.pollFirstEntry());
        System.out.println("Map after polling first entry: " + map);
        System.out.println("Poll Last Entry: " + map.pollLastEntry());
        System.out.println("Map after polling last entry: " + map);
    }
}

Output:

Initial Map: {1=One, 2=Two, 3=Three}
Poll First Entry: 1=One
Map after polling first entry: {2=Two, 3=Three}
Poll Last Entry: 3=Three
Map after polling last entry: {2=Two}

Example 3: Descending Map

This example demonstrates how to get a reverse order view of the map using the descendingMap method.

import java.util.NavigableMap;
import java.util.TreeMap;

public class DescendingMapExample {
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, "Three");

        NavigableMap<Integer, String> descendingMap = map.descendingMap();
        System.out.println("Original Map: " + map);
        System.out.println("Descending Map: " + descendingMap);
    }
}

Output:

Original Map: {1=One, 2=Two, 3=Three}
Descending Map: {3=Three, 2=Two, 1=One}

Example 4: Navigable Key Set

This example shows how to use the navigableKeySet method to get a NavigableSet view of the keys.

import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.TreeMap;

public class NavigableKeySetExample {
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, "Three");

        NavigableSet<Integer> keySet = map.navigableKeySet();
        System.out.println("Navigable Key Set: " + keySet);
    }
}

Output:

Navigable Key Set: [1, 2, 3]

Example 5: Submap

This example demonstrates how to use the subMap method to get a view of a portion of the map.

import java.util.NavigableMap;
import java.util.TreeMap;

public class SubMapExample {
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, "Three");
        map.put(4, "Four");
        map.put(5, "Five");

        NavigableMap<Integer, String> subMap = map.subMap(2, true, 4, true);
        System.out.println("Submap from 2 (inclusive) to 4 (inclusive): " + subMap);
    }
}

Output:

Submap from 2 (inclusive) to 4 (inclusive): {2=Two, 3=Three, 4=Four}

4. Conclusion

The NavigableMap interface in Java provides powerful methods for navigating and manipulating sorted maps. It extends the functionality of the SortedMap interface by offering methods to find the closest matches for given search targets, poll entries, and view portions of the map. The examples provided demonstrate common usage patterns and highlight the capabilities of the NavigableMap interface.

Comments