Last active
December 16, 2017 11:24
-
-
Save nijjwal/af49961431c33d84b7e0 to your computer and use it in GitHub Desktop.
HashMap Code. i) Find an employee whose id is 300. ii) Display all the employee information to show that map does not preserve insertion order. iii) Override toString method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.HashMap; | |
public class Employee{ | |
Integer empId; | |
String name; | |
@Override | |
public String toString() | |
{ | |
return "Employee Id: " + empId + " Employee name: " + name; | |
} | |
public static void main(String[] args) | |
{ | |
Map<Integer,Employee> empMap = new HashMap<Integer, Employee>(); | |
Employee emp1 = new Employee(); | |
emp1.name = "Moore"; | |
emp1.empId = 300; | |
Employee emp2 = new Employee(); | |
emp2.name = "Thawn"; | |
emp2.empId = 500; | |
Employee emp3 = new Employee(); | |
emp3.name = "Core"; | |
emp3.empId = 100; | |
empMap.put(emp1.empId, emp1); | |
empMap.put(emp2.empId, emp2); | |
empMap.put(emp3.empId, emp3); | |
System.out.println(empMap.get(300)); | |
//Iterate over each element in map | |
for (Map.Entry<Integer, Employee> entry : empMap.entrySet()) | |
{ | |
System.out.println(entry.getKey() + "/" + entry.getValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment