NavigableMap in Java

A NavigableMap in Java extends a SortedMap with navigation methods returning the closest matches for given search targets. Methods lowerEntry(), floorEntry(), ceilingEntry(), and higherEntry() return Map.Entry objects associated with keys respectively less than, less than or equal, greater than or equal, and greater than a given key, returning null if there is no such key.

Similarly, methods lowerKey(), floorKey(), ceilingKey(), and higherKey() return only the associated keys. All of these methods are designed for locating, not traversing entries.

A NavigableMap may be accessed and traversed in either ascending or descending key order.

This interface additionally defines methods firstEntry(), pollFirstEntry(), lastEntry(), and pollLastEntry() that return and/or remove the least and greatest mappings, if any exist, else returning null.

Implementations of entry-returning methods are expected to return Map.Entry pairs representing snapshots of mappings at the time they were produced, and thus generally do not support the optional Entry.setValue() method.

Note however that it is possible to change mappings in the associated map using method put.

More information could be found here at

Related Posts:

Here is the example of NavigableMap in Java code

public class NavigableMapTest {
	public static void main(String[] args) {
		NavigableMap<String, String> navigableMap = new TreeMap<>();
		navigableMap.put("RedHat", "Unix");
		navigableMap.put("Google", "Android");
		navigableMap.put("Apple", "iOS");
		navigableMap.put("Microsoft", "Windows");
		System.out.println("All entries: " + navigableMap);
		System.out.println("Ceiling key of: " + navigableMap.ceilingKey("RedHat"));
		System.out.println("Higher key than: " + navigableMap.higherKey("Google"));
		System.out.println("Flooer key of: " + navigableMap.floorKey("Apple"));
		System.out.println("Lower key than: " + navigableMap.lowerKey("Microsoft"));
		System.out.println("First entry: " + navigableMap.pollFirstEntry());
		System.out.println("Last entry: " + navigableMap.pollLastEntry());
		System.out.println("Sort in decending order for remaining entries: " + navigableMap.descendingMap());
		System.out.println("Remaining entries: " + navigableMap);

Testing the Application

When you run the above code you will see the following output:

All entries: {Apple=iOS, Google=Android, Microsoft=Windows, RedHat=Unix}
Ceiling key of: RedHat
Higher key than: Microsoft
Flooer key of: Apple
Lower key than: Google
First entry: Apple=iOS
Last entry: RedHat=Unix
Sort in decending order for remaining entries: {Microsoft=Windows, Google=Android}
Remaining entries: {Google=Android, Microsoft=Windows}

That’s all about NavigableMap in Java.

Thanks for reading.

Leave a Comment