TreeMap entrySet() in Java

entrySet(): This method is available in java.util.TreeMap class of Java.

Syntax:

Set<Entry<K, V>> java.util.TreeMap.entrySet()

This method returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key order.

Parameters: NA

Returns: a set view of the mappings contained in this map, sorted in ascending key order.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapentrySet {
    public static void main(String[] args) {

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        System.out.println(treeMap.entrySet());
    }
}

Output:

[2=Hello, 6=C++, 11=Java, 23=Program, 25=Program]


No comments:

Post a Comment