Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation

Java NavigableMap Interface

This tutorial explains the Java NavigableMap interface, its core methods, and how to use it with TreeMap.

The NavigableMap interface of the Java collections framework provides navigation capabilities across map entries. It is a specialized SortedMap, extending its functionality with methods that allow efficient bidirectional traversal.


Class that Implements NavigableMap

Because NavigableMap is an interface, you cannot instantiate it directly. The primary implementation is the TreeMap class, which offers a self‑balancing binary search tree structure that maintains keys in sorted order.

Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation


How to Use NavigableMap

First, import the interface:

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

Then create an instance:

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

In the code above, numbers is a TreeMap that stores key–value pairs. Key is the unique identifier, and Value is the associated data.


Core Methods of NavigableMap

All SortedMap operations are available, but several are overridden to provide tighter control. Key methods include:

headMap(key, booleanInclusive)

Returns a view of the portion of the map whose keys are strictly less than key when booleanInclusive is false (default). If booleanInclusive is true, the returned map includes the entry for key.

tailMap(key, booleanInclusive)

Returns a view of the portion of the map whose keys are greater than or equal to key when booleanInclusive is true (default). Passing false excludes the key entry.

subMap(fromKey, fromInclusive, toKey, toInclusive)

Provides a view between fromKey and toKey. The defaults are fromInclusive = true and toInclusive = false. Adjusting these flags controls whether the boundary keys are part of the resulting map.


Additional Navigation Methods

For the official specification, see the Java NavigableMap documentation.


TreeMap Implementation Example

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

class Main {
    public static void main(String[] args) {
        // Creating NavigableMap using TreeMap
        NavigableMap<String, Integer> numbers = new TreeMap<>();

        // Insert elements into the map
        numbers.put("Two", 2);
        numbers.put("One", 1);
        numbers.put("Three", 3);
        System.out.println("NavigableMap: " + numbers);

        // Access the first entry of the map
        System.out.println("First Entry: " + numbers.firstEntry());

        // Access the last entry of the map
        System.out.println("Last Entry: " + numbers.lastEntry());

        // Remove the first entry from the map
        System.out.println("Removed First Entry: " + numbers.pollFirstEntry());

        // Remove the last entry from the map
        System.out.println("Removed Last Entry: " + numbers.pollLastEntry());
    }
}

Output

NavigableMap: {One=1, Three=3, Two=2}
First Entry: One=1
Last Entry: Two=2
Removed First Entry: One=1
Removed Last Entry: Two=2

Learn more about TreeMap in the Java TreeMap documentation.


With a solid grasp of the NavigableMap interface and its TreeMap implementation, you can build efficient, sorted data structures for your Java applications.

Java

  1. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  2. Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
  3. Java Collection Interface: Core Concepts & Essential Methods
  4. Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
  5. Mastering Java's Deque Interface: Features, Methods, and Practical Examples
  6. Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
  7. Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
  8. Mastering Java’s ConcurrentMap: Thread‑Safe Maps Explained
  9. Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples
  10. Mastering Java SortedSet: A Practical Guide to TreeSet and Its Methods