In this section we will discuss how can size() method be implemented in Map interface in java.
size() method of Map Interface in Java.
In this section we will discuss how can size() method be implemented in Map interface in java.
Syntax
int size()
This method returns the size of the underlying map. This method gives back the total number of ' key-value' associations of the underlying map.
Parameter description
This method has no parameter but, it returns an integer value in terms of the size of the map, contains the mappings into the map.
Example of size() method
In this section we will discuss how does size() method work in Map interface. This example will help you to understand how you can found the total number of mappings contained into the map.
Example :
package devmanuals.com; import java.util.Map; import java.util.HashMap; public class MapSize { public static void main (String args[]){ Map mp=new HashMap(); mp.put(1, "A"); mp.put(2, "B"); mp.put(3, "C"); mp.put(4, "D"); System.out.println("Mapping = "+mp); boolean bol= mp.isEmpty(); if(bol==false) System.out.println("Size of map = "+mp.size()); else System.out.println("Map has no mappings"); } }
Output :
Mapping = {1=A, 2=B, 3=C, 4=D} Size of map = 4 |
[ 0 ] Comments