Dictionary size() Method in Java with Examples
The size() Method of Dictionary class in Java is used to know the size of the dictionary or the number of distinct keys present in the dictionary.
Syntax:
DICTIONARY.size()
Parameters:
The method does not accept any parameters.
Return Value:
The method returns the number of keys present in the dictionary.
Below programs illustrate the size() Method of Dictionary:
Program 1:
import java.util.*;
public class Dictionary_Demo {
public static void main(String args[])
{
Dictionary<Integer, String> dict
= new Hashtable<Integer, String>();
dict.put( 10 , "Geeks" );
dict.put( 15 , "4" );
dict.put( 10 , "Geeks" );
dict.put( 25 , "Welcomes" );
dict.put( 30 , "You" );
System.out.println( "Dictionary: " + dict);
System.out.println( "The size of the dictionary is "
+ dict.size());
}
}
|
Output:
Dictionary: {10=Geeks, 30=You, 15=4, 25=Welcomes}
The size of the dictionary is 4
Program 2:
import java.util.*;
public class Dictionary_Demo {
public static void main(String args[])
{
Dictionary<String, Integer> dict
= new Hashtable<String, Integer>();
dict.put( "Geek" , 10 );
dict.put( "4" , 15 );
dict.put( "Geeks" , 20 );
dict.put( "Welcomes" , 25 );
dict.put( "You" , 30 );
System.out.println( "Dictionary: " + dict);
System.out.println( "The size of the dictionary is "
+ dict.size());
}
}
|
Output:
Dictionary: {You=30, Welcomes=25, 4=15, Geeks=20, Geek=10}
The size of the dictionary is 5
Last Updated :
27 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...