Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
mapping
keyvalue
communitycreator

What is AbstractMap.size() in Java?

Hammad Nasir

AbstactMap.size() in Java is a method of the AbstractMap class. The method is used to get the key-value mappings in a map.

Syntax


public int size()

Parameters

The AbstactMap.size()method takes in no parameters.

Return value

The AbstactMap.size() method returns the size of the key-value mappings in the map.


If the map has more than Integer.MAX_VALUE element, then the method returns Integer.MAX_VALUE.

Code

import java.util.*;
  
public class Abstract_Map_Class{
    public static void main(String[] args)
    {
        AbstractMap<Integer, String> temp_absMap = new TreeMap<Integer, String>();
        int size = temp_absMap.size();
        
        System.out.println("The size of the map is "  + temp_absMap.size());
    
        temp_absMap.put(1, "Hello");
        temp_absMap.put(2, "from");
        temp_absMap.put(3, "Educative");
        
        System.out.println("Mapping is : " + temp_absMap);
        
        size = temp_absMap.size();    
        System.out.println("The size of the map is "  + temp_absMap.size());
    }
}

Explanation

  • First, create temp_absMap, an empty AbstractMap class object.

  • Then use the AbstractMap.size() method to check the size of temp_absMap and store it in the variable size.

  • Next, use println() to display the size of the temp_absmap.

  • Then populateadd values/data the temp_absmap by mapping string values to int keys.

  • Use println() again to display the temp_absmap.

  • Next, use the AbstractMap.size() method again to store the size of the temp_absmap after it has key-value pairs mapping stored in it.

  • Lastly, we use println() to display the size of the temp_absmap.

RELATED TAGS

java
mapping
keyvalue
communitycreator
RELATED COURSES

View all Courses

Keep Exploring