HashMap in Java
Map in Java has Key-Value pairs
To access the value you must know the Key in the HashMap. Hashmap uses a technique of Hashing
HashMap does not allow duplicates for the Keys.
Below is the simple HashMap program in Java to demonstrate the Key-Value objects in HashMap
HashExMain.java:
EmployeeDetails.java:
To access the value you must know the Key in the HashMap. Hashmap uses a technique of Hashing
HashMap does not allow duplicates for the Keys.
Below is the simple HashMap program in Java to demonstrate the Key-Value objects in HashMap
HashExMain.java:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashExMain {
public static void main (String [] args){
HashMap<Integer, EmployeeDetails> map = new HashMap<Integer, EmployeeDetails>();
EmployeeDetails empOne = new EmployeeDetails(101, "Andrew Heins");
EmployeeDetails empTwo = new EmployeeDetails(102, "Bill patrick");
map.put(1, empOne);
map.put(2, empTwo);
for (HashMap.Entry<Integer, EmployeeDetails> entry : map.entrySet())
{
EmployeeDetails e = entry.getValue();
System.out.println("The Key is " + entry.getKey() + " " + "& " + "The value is " + e.emplNo + " &" + " " + e.emplName);
}
}
}
EmployeeDetails.java:
public class EmployeeDetails {
int emplNo;
String emplName;
public EmployeeDetails(int emplNo, String emplName){
this.emplNo = emplNo;
this.emplName = emplName;
}
}
Getting ConcurrentModificationException ? Check out how to avoid this exception in the blog post
ConcurrentHashMap
Getting ConcurrentModificationException ? Check out how to avoid this exception in the blog post
ConcurrentHashMap
Comments
Post a Comment