TreeMap size() in Java

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

Syntax:

int java.util.TreeMap.size()

This method returns the number of key-value mappings in this map.

Parameters: NA

Returns: the number of key-value mappings in this map.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapsize {
    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.size());

    }
}

Output:

5


No comments:

Post a Comment