DictionaryClass and its methods
Before going into DictionaryClass let us discuss about Map Hierarchy
Map Hierarchy
The below diagram shows the Map hierarchy consisting of classes and interfaces.
Now let us discuss about DictionaryClass and its methods
What is DictionaryClass?
DictionaryClass is the implemented class of Map interface. It is an abstract class as a result its object cannot be created directly instead we have to upcast its child classes like Hashtable to Dictionary class for object creation. It is a Legacy class found in jdk 1.0 version which stores pairs of keys and values. Using Dictionary class we can remove a specific key value pair,display a particular key value pair and add new key value pairs. Dictionary class is found in java.utils package.Since Dictionary class is a legacy class so it is always thread safe and synchronized. It has no specific growth strategy and threshold is not applicable.
Methods of DictionaryClass
Below are some of the methods of DictionaryClass:
Method Name | Description |
put(Object key, Object value) | It is used to add a pair of key and value into the DictionaryClass. |
size() | It is used to display the size of the key and value pairs present in DictionaryClass. |
equals(Object o) | It is used to check if key and value pairs present in one DictionaryClass is equal to the key and value pairs present in another DictionaryClass. It gives ‘true’ value if key and value pairs present in two DictionaryClasss are equal and ‘false’ value if key and value pairs present in two DictionaryClasss are not equal. |
get(Object key) | It is used to display the value of a specified key among all key and value pairs of HashSet. It displays ‘null’ if the specified key is not present among all key and value pairs of DictionaryClass. |
isEmpty() | It is used to check if the DictionaryClass class is empty or not. It display ‘true’ value if the DictionaryClass class is empty and ‘false’ value if the DictionaryClass class is not empty. |
remove(Object key) | It is used to remove a specified key and value pair from the DictionaryClass. If we try to print a key which is not present in DictionaryClass it will display null value but if we try to print a key which is present in DictionaryClass it will display its corresponding value. |
elements() | It is used to display the values of the key and value pairs present in Dictionary class in a sequential way. |
keys() | It is used to display the keys of the key and value pairs present in Dictionary class in a sequential way. |
1. Let us check how put(Object key, Object value) method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
System.out.println(m1);
}
}
Output: {Roll No=1101219512, Pin Number=3434, Registration Number=1653005, Student ID=25, Postal code=751121}
Screenshot:
2. Let us check how size() method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
System.out.println(m1.size());
}
}
Output: 5
Screenshot:
3. Let us check how equals(Object o) method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
Dictionary m2= new Hashtable();
m2.put("Roll No",1101219512 );
m2.put("Registration Number", 1653005);
m2.put("Pin Number", 3434);
m2.put("Student ID",25 );
m2.put("Postal code",751121);
Dictionary m3= new Hashtable();
m3.put("Roll No",1101219512 );
m3.put("Registration Number", 1653005);
m3.put("Pin Number", 3434);
m3.put("Student ID",25 );
m3.put("Postal code",751121);
m3.put("Flat No",01);
System.out.println("Is DictionaryClass m1 equals m2 ?: "+m1.equals(m2));
System.out.println("Is DictionaryClass m1 equals m3 ?: "+m1.equals(m3));
}
}
Output: Is DictionaryClass m1 equals m2 ?: true
Is DictionaryClass m1 equals m3 ?: false
Screenshot:
4. Let us check how get(Object key) method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
System.out.println(m1.get("Pin Number"));
System.out.println(m1.get("Registration Number"));
}
}
Output:
3434
1653005
Screenshot:
5. Let us check how isEmpty() method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
System.out.println("Is DictionaryClass m1 empty? :" +m1.isEmpty());
Dictionary m2= new Hashtable();
System.out.println("Is DictionaryClass m2 empty? :" +m2.isEmpty());
}
}
Output: Is DictionaryClass m1 empty? :false
Is DictionaryClass m2 empty? :true
Screenshot:
6. Let us check how remove(Object key) method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
System.out.println(m1);
m1.remove("Registration Number");
System.out.println("New key value pairs are:");
System.out.println(m1);
System.out.println(m1.remove("Flat No"));
System.out.println(m1.remove("Student ID"));
}
}
Output:
{Roll No=1101219512, Pin Number=3434, Registration Number=1653005, Student ID=25, Postal code=751121}
New key value pairs are:
{Roll No=1101219512, Pin Number=3434, Student ID=25, Postal code=751121}
null
25
Screenshot:
7. Let us check how elements() method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
Enumeration e= m1.elements();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
}
}
Output:
1101219512
3434
1653005
25
751121
Screenshot:
8. Let us check how keys() method works in Dictionary class.
Code Snippet:
package dictionaryClass;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
public class DictionaryClass {
public static void main(String[] args) {
Dictionary m1= new Hashtable();
m1.put("Roll No",1101219512 );
m1.put("Registration Number", 1653005);
m1.put("Pin Number", 3434);
m1.put("Student ID",25 );
m1.put("Postal code",751121);
Enumeration e= m1.keys();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
}
}
Output:
Roll No
Pin Number
Registration Number
Student ID
Postal code
Screenshot:
Difference between HashMap and DictionaryClass
SL NO: | HashMap | DictionaryClass |
1 | It is not a legacy class | It is a legacy class |
2 | Introduced in jdk version 1.2 | Introduced in jdk version 1.0 |
3 | It is neither thread safe nor synchronized | It is thread safe and synchronized |
4 | It’s growth strategy is that its capacity can be increased on reaching load factor 0.75 | It has no growth strategy |
5 | It’s threshold is 0.75 times current capacity | It has no threshold. |
Conclusion:
DictionaryClass class is used for handling all the key and value pairs in an effective way. It is necessary to understand the usage of all the methods of the DictionaryClass class for better handling of DictionaryClass elements. By thoroughly understanding the DictionaryClass class and its methods developers and testers can utitlise those concepts in their real life tasks. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process.Automation testers can utlize all the methods of DictionaryClass class to create better and effective test scripts for automation testing of web applications and websites.
Also, read my blogs on:
Consult Us