A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/sortedmap-tailmap-method-in-java/ below:

SortedMap tailMap() method in Java

SortedMap tailMap() method in Java

Last Updated : 26 Nov, 2018

The tailMap() method of

SortedMap interface in Java

is used to return a view of the portion of this map whose keys are greater than or equal to fromKey.

Note

: The map returned by this method will throw an IllegalArgumentException if an attempt is made to insert a key outside its range.

Syntax

:

SortedMap<K, V> tailMap(K fromKey)

Where, K is the type of key maintained by this Set and V is the type of values associated with the Key.

Parameters

: This function accepts a single parameter

fromKey

which represents high endpoint (exclusive) of the keys in the returned map.

Return Value

: It returns a view of the portion of this map whose keys are strictly greater than or equal to fromKey.

Exception

:

Below programs illustrate the above method:

Program 1

:

Java
// A Java program to demonstrate
// working of SortedSet
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Create a TreeSet and inserting elements
        SortedMap<Integer, String> mp = new TreeMap<>();

        // Adding Element to SortedSet
        mp.put(1, "One");
        mp.put(2, "Two");
        mp.put(3, "Three");
        mp.put(4, "Four");
        mp.put(5, "Five");

        // Returning the key greater
        // than or equal to 2
        System.out.print("Last Key in the map is : "
                         + mp.tailMap(2));
    }
}
Output:
Last Key in the map is : {2=Two, 3=Three, 4=Four, 5=Five}
Program 2

:

Java
// A Java program to demonstrate
// working of SortedSet
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Create a TreeSet and inserting elements
        SortedMap<String, String> mp = new TreeMap<>();

        // Adding Element to SortedSet
        mp.put("One", "Geeks");
        mp.put("Two", "For");
        mp.put("Three", "Geeks");
        mp.put("Four", "Code");
        mp.put("Five", "It");

        // Returning the key greater
        // than or equal to
        System.out.print("Last Key in the map is : "
                         + mp.tailMap("D"));
    }
}
Output:
Last Key in the map is : {Five=It, Four=Code, One=Geeks, Three=Geeks, Two=For}
Reference

:

https://docs.oracle.com/javase/10/docs/api/java/util/SortedMap.html#tailMap(K)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4