• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » NavigableMap in Java

NavigableMap in Java

Last Updated on: May 2, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Implementing Classes
  • Basic Operations on NavigableMap

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. Because It provides convenient navigation methods like lowerKey, floorKey, ceilingKey and higherKey, making it easy to find the information you need.

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 in order to make use of its functionality.

Declaration:

In Java, we can use the NavigableMap 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 its keys, or by a Comparator provided at map creation time.

Java Tutorial
  • SortedMap In Java
  • Map In Java
  • Java ArrayList
  • SortedSet In Java
  • Java HashSet
  • LinkedHashSet In Java
  • Java TreeSet

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. If no Comparator is provided at the time of construction, then the natural order will be followed.

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 be used to 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. The value of each key is found by 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);
		}
	}
}

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers