HashMap size() in Java

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

Syntax:

int java.util.HashMap.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.HashMap;

public class HashMapsize {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap =
new HashMap<String, Integer>();

        hashMap.put("Hello", 1);
        hashMap.put("World", 2);

        System.out.println(hashMap.size());

    }
}

Output:

2


No comments:

Post a Comment