Map size() Method in Java With Examples Last Updated : 02 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Map size() method in Java is used to get the total number entries i.e, key-value pair. So this method is useful when you want total entries present on the map. If the map contains more than Integer.MAX_VALUE elements returnInteger.MAX_VALUE Syntax: int size(); Parameter: This method does not take any parameter. Return value: This method returns the number of key-value mappings in this map. Example 1: For non-generic input Java // Java program to illustrate // the Map size() Method import java.util.*; public class MapSizeExample { // Main Method public static void main(String[] args) { Map map = new HashMap(); // Adding key-values map.put(1, "Amit"); map.put(5, "Rahul"); map.put(2, "Jai"); map.put(6, "Amit"); // using the method System.out.println("Size of the map is : " + map.size()); } } Output: Size of the map is : 4Example 2: For Generic Input Java // Java program to illustrate // the Map size() Method import java.util.*; class MapSizeExample { // Main Method public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); map.put(4, "four"); // using the method System.out.println("Size of map is :" + map.size()); } } Output: Size of the map is : 4 Comment More infoAdvertise with us Next Article Map put() Method in Java with Examples A AshishVishwakarma1 Follow Improve Article Tags : Java Java-Collections java-map Java-Map-Programs Practice Tags : JavaJava-Collections Similar Reads Map put() Method in Java with Examples The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java. If the map already contains the mapping for the specified key, the old value is replaced by the new specified value. Example: Java / 3 min read Map put() Method in Java with Examples The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java. If the map already contains the mapping for the specified key, the old value is replaced by the new specified value. Example: Java / 3 min read Map remove() Method in Java with Examples The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. Syntax: V remove(Object key)Parameters: This method has the only argument key, whose mapping is to be removed from the map.Returns: This method returns the value to which this map previously as 2 min read Map Values() Method in Java With Examples Map Values() method returns the Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Syntax: map.values() Parameter: This method does not take any parameter. Return value: It returns a collect 2 min read Map putAll() Method in Java with Examples This method is used to copy all of the mappings from the specified map to this map. Syntax: void putAll(Map m) Parameters: This method has the only argument map m, which contains key-value mappings to be copied to given map. Returns: This method returns previous value associated with the key if pres 2 min read Map clear() method in Java with Example The java.util.Map.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate 2 min read Like