TreeMap values() in Java

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

Syntax:

Collection<V> java.util.TreeMap.values()

This method returns a Collection view of the values contained in this map. The collection's iterator returns the values in ascending order of the corresponding keys.

Parameters: NA

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

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapvalues {
    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.values());

    }
}

Output:

[Hello, C++, Java, Program, Program]


No comments:

Post a Comment