Java TreeMap navigableKeySet() Method

The TreeMap.navigableKeySet() method in Java is used to retrieve a NavigableSet view of the keys contained in the TreeMap. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.navigableKeySet() can be used effectively.

Table of Contents

  1. Introduction
  2. navigableKeySet Method Syntax
  3. Examples
    • Retrieving the Navigable Key Set from a TreeMap
    • Iterating Over the Navigable Key Set
  4. Real-World Use Case
    • Example: Managing a Set of Student Names
  5. Conclusion

Introduction

The TreeMap.navigableKeySet() method is a member of the TreeMap class in Java. It provides a way to retrieve a NavigableSet view of the keys contained in the TreeMap. This set is backed by the map, so changes to the map are reflected in the set, and vice versa. The keys in the NavigableSet are sorted in their natural order or according to the comparator used by the TreeMap.

navigableKeySet() Method Syntax

The syntax for the navigableKeySet method is as follows:

public NavigableSet<K> navigableKeySet()
  • The method does not take any parameters.
  • The method returns a NavigableSet<K> view of the keys contained in this map.

Examples

Retrieving the Navigable Key Set from a TreeMap

The navigableKeySet method can be used to get a NavigableSet view of the keys in a TreeMap.

Example

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

public class NavigableKeySetExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Retrieving the navigable key set from the TreeMap
        NavigableSet<String> navigableKeySet = treeMap.navigableKeySet();

        // Printing the navigable key set
        System.out.println("Navigable Key Set: " + navigableKeySet);
    }
}

Output:

Navigable Key Set: [Anita, Priya, Ravi, Suresh, Vijay]

Iterating Over the Navigable Key Set

You can also iterate over the keys retrieved from a TreeMap using the navigableKeySet method.

Example

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

public class IterateNavigableKeySetExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Retrieving the navigable key set from the TreeMap
        NavigableSet<String> navigableKeySet = treeMap.navigableKeySet();

        // Iterating over the navigable key set
        System.out.println("Iterating over navigable key set:");
        for (String key : navigableKeySet) {
            System.out.println(key);
        }
    }
}

Output:

Iterating over navigable key set:
Anita
Priya
Ravi
Suresh
Vijay

Real-World Use Case

Example: Managing a Set of Student Names

A common real-world use case for TreeMap.navigableKeySet() is managing a set of student names where you need a sorted view of the names.

Example

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

public class StudentManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage student names and their IDs
        TreeMap<String, Integer> students = new TreeMap<>();

        // Adding student names and their IDs to the TreeMap
        students.put("Ravi", 101);
        students.put("Priya", 102);
        students.put("Vijay", 103);
        students.put("Anita", 104);
        students.put("Suresh", 105);

        // Retrieving the navigable key set from the TreeMap
        NavigableSet<String> studentNames = students.navigableKeySet();

        // Printing the student names
        System.out.println("Student Names: ");
        for (String name : studentNames) {
            System.out.println(name);
        }
    }
}

Output:

Student Names:
Anita
Priya
Ravi
Suresh
Vijay

In this example, TreeMap.navigableKeySet() is used to manage and retrieve a sorted set of student names, making it easy to display and process the list of names in a sorted order.

Conclusion

The TreeMap.navigableKeySet() method in Java provides a way to retrieve a NavigableSet view of the keys contained in a TreeMap. By understanding how to use this method, you can efficiently manage collections of keys in your Java applications. The method allows you to access and manipulate the keys in the map, making it a versatile tool for data management in various scenarios.

Leave a Comment

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

Scroll to Top