Java TreeMap headMap() Method

The TreeMap.headMap() method in Java is used to retrieve a view of the portion of the map whose keys are strictly less than a specified key. We will also cover a real-world use case to show how TreeMap.headMap() can be used effectively.

Table of Contents

  1. Introduction
  2. headMap Method Syntax
    • SortedMap<K,V> headMap(K toKey)
    • NavigableMap<K,V> headMap(K toKey, boolean inclusive)
  3. Examples
    • Using SortedMap.headMap()
    • Using NavigableMap.headMap()
  4. Real-World Use Case
    • Example: Retrieving a Subset of Contacts
  5. Conclusion

Introduction

The TreeMap.headMap() 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 an upper bound key. This method can be used to create a subset of the map with keys that are strictly less than or less than or equal to the specified key, depending on the method used.

headMap() Method Syntax

SortedMap<K,V> headMap(K toKey)

This method returns a view of the portion of this map whose keys are strictly less than toKey.

Syntax

public SortedMap<K,V> headMap(K toKey)
  • Parameters:
    • toKey: The high endpoint (exclusive) of the keys in the returned map.
  • Returns: A SortedMap view of the specified portion of this map.

NavigableMap<K,V> headMap(K toKey, boolean inclusive)

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

Syntax

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

Examples

Using SortedMap.headMap()

Example

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

public class SortedMapHeadMapExample {
    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 head map whose keys are strictly less than "Vijay"
        SortedMap<String, Integer> headMap = treeMap.headMap("Vijay");

        // Printing the head map
        System.out.println("HeadMap (keys strictly less than 'Vijay'): " + headMap);
    }
}

Output:

HeadMap (keys strictly less than 'Vijay'): {Anita=28, Priya=30, Ravi=25}

Using NavigableMap.headMap()

Example

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

public class NavigableMapHeadMapExample {
    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 head map whose keys are less than or equal to "Vijay"
        NavigableMap<String, Integer> headMap = treeMap.headMap("Vijay", true);

        // Printing the head map
        System.out.println("HeadMap (keys less than or equal to 'Vijay'): " + headMap);
    }
}

Output:

HeadMap (keys less than or equal to 'Vijay'): {Anita=28, Priya=30, Ravi=25, Vijay=35}

Real-World Use Case

Example: Retrieving a Subset of Contacts

A common real-world use case for TreeMap.headMap() is managing a contact list where you need to retrieve a subset of contacts based on their names.

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 a subset of contacts whose names are less than or equal to "Ravi"
        NavigableMap<String, Integer> subContacts = contacts.headMap("Ravi", true);

        // Printing the subset of contacts
        System.out.println("Subset of contacts (names less than or equal to 'Ravi'): ");
        for (Map.Entry<String, Integer> entry : subContacts.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Subset of contacts (names less than or equal to 'Ravi'):
Anita: 4445556666
Priya: 9876543210
Ravi: 1234567890

In this example, TreeMap.headMap() is used to retrieve a subset of contacts based on their names, making it easy to manage and view a specific portion of the contact list.

Conclusion

The TreeMap.headMap() method in Java provides a way to obtain a view of a portion of the map based on an upper bound 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