NavigableMap in Java

Welcome to this tutorial on the Java NavigableMap interface. We’ll be learning about its methods through a simple example.

The NavigableMap interface is a powerful tool for managing data in Java applications. The NavigableMap interface in the Java collections framework allows you to move around among the entries in a map. It provides convenient navigation methods like lowerKey, floorKey, ceilingKey, and higherKey, making finding the information you need easy.

Post On:NavigableMap in Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

Since NavigableMap is an interface, we cannot create objects from it directly. However, we can use the TreeMap class that implements NavigableMap to use its functionality.

Declaration:

We can use the NavigableMap in Java by importing the java.util.NavigableMap package. To create a navigable map, we first need to import the package. Once we have imported it, here’s how to create a navigable map:

// NavigableMap implementation by TreeMap class
NavigableMap<Key, Value> obj = new TreeMap<>();

Implementing Classes

There are two implementing classes for the NavigableMap interface: ConcurrentSkipListMap and TreeMap.

TreeMap is a NavigableMap implementation that uses a Red-Black tree. In Treemap, elements are sorted according to the natural ordering of their keys or by a Comparator provided at map creation time.

Basic Operations on NavigableMap

Adding Elements:

There are several ways to add elements to a NavigableMap. The code below shows how to use some of the methods available in the Map interface. As you can see from the code, the insertion order is not retained when using these methods. The natural order will be followed if no Comparator is provided during construction.

package com.SoftwareTestingO.collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapAdding 
{
	public static void main(String[] args) 
	{
		// Initializing Objects
		NavigableMap<Integer, String> nmap=new TreeMap<Integer, String>();

		// Add elements using put()
		nmap.put(2, "Testingo");
		nmap.put(3, "Blog");
		nmap.put(1, "Software");

		// Print the contents
		System.out.println("All The Values : "+ nmap);
	}
}

Removing Elements

To remove elements from a NavigableMap, you can use the remove() method to remove a mapping for a specific key or the clear() method to remove all mappings.

package com.SoftwareTestingO.collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapRemove 
{
	public static void main(String[] args) 
	{
		// Initializing Objects
		NavigableMap<Integer, String> nmap=new TreeMap<Integer, String>();

		// Add elements using put()
		nmap.put(2, "Testingo");
		nmap.put(3, "Blog");
		nmap.put(1, "Software");
		nmap.put(4, "Software");

		// Print the contents
		System.out.println("All The Values : "+ nmap);
		
		// Remove a Value
		nmap.remove(4);
		
		// Remove a Specific Value
		System.out.println("After Remove The Values : "+ nmap);
		
		//Clear All the Value
		nmap.clear();
		
		// After Clear
		System.out.println("After Clear All Values : "+ nmap);
	}
}

Accessing the Elements Using Key

You can access the elements of a NavigableMap using the get() method. For example:

package com.SoftwareTestingO.collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapAccessing 
{
	public static void main(String[] args) 
	{
		// Initializing Objects
		NavigableMap<Integer, String> nmap=new TreeMap<Integer, String>();

		// Add elements using put()
		nmap.put(2, "Testingo");
		nmap.put(3, "Blog");
		nmap.put(1, "Software");

		// Accessing the Elements using key
		System.out.println("Accessed Value:  "+ nmap.get(3));

		// Display the set of keys using keySet()
		System.out.println("\nThe NavigableMap key set: "+ nmap.keySet());
	}
}

Traversing

The Iterator interface can traverse any structure in the Collection Framework. Since Iterators work with one type of data, Entry< ? , ? > is used to resolve the two separate types into a compatible format. Then, the next() method is used to print the elements of the NavigableMap. Another famous way is to use a for-each loop and get the keys. Each key’s value is found using the getValue() method.

package com.SoftwareTestingO.collections;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapTraverse 
{
	public static void main(String[] args) 
	{
		// Initializing Objects
		NavigableMap<Integer, String> nmap=new TreeMap<Integer, String>();

		// Add elements using put()
		nmap.put(2, "Testingo");
		nmap.put(3, "Blog");
		nmap.put(1, "Software");

		// Create an Iterator
		Iterator<NavigableMap.Entry<Integer, String> > itr= nmap.entrySet().iterator();

		// Iterator Using hasNext()
		while (itr.hasNext()) 
		{
			NavigableMap.Entry<Integer, String> entry= itr.next();
			System.out.println("Key = " + entry.getKey()+ ", Value = "+ entry.getValue());
		}

		System.out.println("--------------------------------");
		// Iterate using for-each loop
		for (Map.Entry mapElement : nmap.entrySet()) 
		{
			// get the key using getKey()
			int key = (int)mapElement.getKey();

			// Finding the value
			String value = (String)mapElement.getValue();

			System.out.println("Key = " + key+ ", Value = " + value);
		}
	}
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment