In this tutorial we will learn about java hashmap size() method . We will see how to calculate size of hashmap using java map size method . Let,s first discuss syntax of java hashmap size() method
Syntax :
public int size();
Parameter : It does not accept any parameter.
Return Value : It returns the number of key-value mappings in this map .
Java HashMap size Method Example
Now we will discuss different example of map.size() method in java.We will see how is the size of Hashmap with String and Integer mapping or vice versa and size of empty Hashmap .
Example 1 : HashMap with String keys and Integer values
In this example we will create HashMap with String and Integer mapping . We add element in map using put method . To get the java map size we will use map.size() method . size() method return 5 because there are 5 key -value mapping .
import java.util.HashMap;
import java.util.Map;
public class HashmapSize {
public static void main(String[] args) {
Map<String,Integer> hm = new HashMap<String,Integer>();
hm.put("Java",1);
hm.put("Vogue",2);
hm.put("Welcomes",3);
hm.put("You",4);
hm.put("Map", 5);
//print HashMap
System.out.println("HashMap :"+ hm);
//Returns the number of key-value mappings in this map
int size = hm.size();
//print HashMap
System.out.println("Size of HashMap is : "+ size);
}
}
Output:
HashMap :{Java=1, Vogue=2, Map=5, You=4, Welcomes=3}
Size of HashMap is : 5
Example 2 : HashMap with Integer keys and String values
In this example we will create HashMap with Integer and String mapping . We add element in HashMap using put method . To get the java map size we will use HashMap size() method . size() method return 5 because there are 5 key -value mapping. Let’s see java map size program with Integer keys and String values .
import java.util.HashMap;
import java.util.Map;
public class HashmapSize1 {
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,"java");
hm.put(2, "vogue");
hm.put(3,"welcomes");
hm.put(4,"you");
hm.put(5,"Map");
//print HashMap
System.out.println("HashMap : "+ hm);
//Returns the number of key-value mappings in this map
int size = hm.size();
//print HashMap
System.out.println("Size of HashMap is : "+ size);
}
}
Output:
HashMap : {1=java, 2=vogue, 3=welcomes, 4=you, 5=Map}
Size of HashMap is : 5
Example 3 : Size Of Empty HashMap
In this example we will create HashMap with String and Integer mapping . We will not add any element into HashMap . To get the java map size we will use HashMap size() method . size() method return 0 because there are 0 key -value mapping. Let’s see java program for java map size of empty Map.
import java.util.HashMap;
import java.util.Map;
public class HashmapSize2 {
public static void main(String[] args) {
Map<String,Integer> hm = new HashMap<String,Integer>();
//print HashMap
System.out.println("HashMap :"+ hm);
//Returns the number of key-value mappings in this map
int size = hm.size();
//print HashMap
System.out.println("Size of HashMap is : "+ size);
}
}
Output:
HashMap :{}
Size of HashMap is : 0
In this tutorial we learned different example of java map size . We used java HashMap size() method with String and Integer mapping and vice versa . We have also calculate size of empty HashMap . Now you can use hashmap size . you can see more java HashMap example for practice .