Java TreeMap tailMap() Method

The TreeMap.tailMap() method in Java allows you to retrieve a view of a portion of the map whose keys are greater than or equal to a specified key. 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.tailMap() can be used effectively.

Table of Contents

  1. Introduction
  2. tailMap Method Syntax
    • SortedMap<K,V> tailMap(K fromKey)
    • NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
  3. Examples
    • Using SortedMap.tailMap()
    • Using NavigableMap.tailMap()
  4. Real-World Use Case
    • Example: Retrieving Contacts Starting from a Certain Name
  5. Conclusion

Introduction

The TreeMap.tailMap() method is a member of the TreeMap class in Java. It provides a way to obtain a view of a specific portion of the map based on a starting key. This method can be used to create a subset of the map with keys that are greater than or equal to the specified key.

tailMap() Method Syntax

SortedMap<K,V> tailMap(K fromKey)

This method returns a view of the portion of this map whose keys are greater than or equal to fromKey.

Syntax

public SortedMap<K,V> tailMap(K fromKey)
  • Parameters:
    • fromKey: The low endpoint (inclusive) of the keys in the returned map.
  • Returns: A SortedMap view of the specified portion of this map.

NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)

This method returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.

Syntax

public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
  • Parameters:
    • fromKey: The low endpoint of the keys in the returned map.
    • inclusive: true if the low endpoint is to be included in the returned view.
  • Returns: A NavigableMap view of the specified portion of this map.

Examples

Using SortedMap.tailMap()

Example

import java.util.TreeMap;
import java.util.SortedMap;

public class SortedMapTailMapExample {
    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);

        // Getting a tail map from "Priya" (inclusive)
        SortedMap<String, Integer> tailMap = treeMap.tailMap("Priya");

        // Printing the tail map
        System.out.println("TailMap from 'Priya': " + tailMap);
    }
}

Output:

TailMap from 'Priya': {Priya=30, Ravi=25, Suresh=40, Vijay=35}

Using NavigableMap.tailMap()

Example

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

public class NavigableMapTailMapExample {
    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);

        // Getting a tail map from "Priya" (exclusive)
        NavigableMap<String, Integer> tailMap = treeMap.tailMap("Priya", false);

        // Printing the tail map
        System.out.println("TailMap from 'Priya' (exclusive): " + tailMap);
    }
}

Output:

TailMap from 'Priya' (exclusive): {Ravi=25, Suresh=40, Vijay=35}

Real-World Use Case

Example: Retrieving Contacts Starting from a Certain Name

A common real-world use case for TreeMap.tailMap() is managing a contact list where you need to retrieve contacts starting from a specific name.

Example

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

public class ContactManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage contacts
        TreeMap<String, Integer> contacts = new TreeMap<>();

        // Adding contacts to the TreeMap
        contacts.put("Ravi", 1234567890);
        contacts.put("Priya", 9876543210);
        contacts.put("Vijay", 5556667777);
        contacts.put("Anita", 4445556666);
        contacts.put("Suresh", 6667778888);

        // Retrieving contacts starting from "Priya" (inclusive)
        NavigableMap<String, Integer> subContacts = contacts.tailMap("Priya", true);

        // Printing the subset of contacts
        System.out.println("Contacts starting from 'Priya': ");
        for (Map.Entry<String, Integer> entry : subContacts.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Contacts starting from 'Priya':
Priya: 9876543210
Ravi: 1234567890
Suresh: 6667778888
Vijay: 5556667777

In this example, TreeMap.tailMap() is used to retrieve contacts starting from a specific name, making it easy to manage and view a specific portion of the contact list.

Conclusion

The TreeMap.tailMap() method in Java provides a way to obtain a view of a portion of the map based on a starting key. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications. The method allows you to create subsets of 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