Working with Maps in Java
//Map Interface has three implementations ie. HashMap, LinkedHashMap and TreeMap.
//Map stores elements in key value pair. Each key and value pair is known as entry. Keys are always unique.
//Retrieving of elements in HashMap is in random order of keys.
//Retrieving of elements in LinlkedHashMap is in insertion order of keys.
// Retrieving of elements in TreeMap is in ascending order of keys.
//Following is a sample of implementation of java HashMap
public class MapExample {
public static void main(String[] args) {
Map mapObj = new HashMap();
mapObj.put(100, "amit");
mapObj.put(101,"Vijay");
mapObj.put(102,"Rahul");
mapObj.put(500,"Vijay");
//Here value is replaced for already existing key.
mapObj.put(99,"sumit");
mapObj.put(99,"rahul");
mapObj.put(99,"luke");
//one null key is allowed in HashMap and LinkedHashMap but not in TreeMap.
mapObj.put(null,"Rahul2");
mapObj.put(null,"Rahul1");
//duplicate values are allowed in map.
mapObj.put(933,null);
mapObj.put(993,null);
// iterate map object with keySet() method of Map interface. This returns set of keys.
Set ks = mapObj.keySet();
for(Integer object : ks){
System.out.println(object+" "+mapObj.get(object));
}
System.out.println();
// iterate map object with entrySet() method of Map interface. This returns set of entries. Each entry is object of Map.Entry interface.
Set> entrySet = mapObj.entrySet();
for(Map.Entry sampleEntry: entrySet){
System.out.println(sampleEntry.getKey()+" "+sampleEntry.getValue());
}
System.out.println();
}
}
// Output will be:
null Rahul1
993 null
99 luke
100 amit
500 Vijay
101 Vijay
933 null
102 Rahul
993 null
99 luke
100 amit
500 Vijay
101 Vijay
933 null
102 Rahul
null Rahul1
993 null
99 luke
100 amit
500 Vijay
101 Vijay
933 null
102 Rahul
993 null
99 luke
100 amit
500 Vijay
101 Vijay
933 null
102 Rahul
***************************************************************************************QACult Best Software Testing Courses in Chandigarh tricity.- We love to enhance your knowledge.
QACult is the premier institute catering to the requirements of experienced and fresh pass-out that gives leaders like you a new way to experience Quality engineering—while you work and giving you the flexibility to both advance your career.
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
And visit for website: www.qacult.com for various blogs and Upcoming Events.
Comments
Post a Comment