The objective of this module is to get hands-on experience with the following topics:
- Introduction to Collection Framework
- Working with Set
- Working with List
- Working with Map
- Working with Generics
- Understand and implement
Map
in Java.
Create a Map
of Employees
with empId
as the key and Employee
object as the value, and iterate through the Map
.
-
Create the following property under the
EmployeeRepository
class:Map<Integer, Employee> employeeMap;
-
Inside the no-argument constructor, perform the following steps:
- Initialize
employeeMap
toHashMap
. - Add all
Employee
objects,Developer
objects, andDesigner
objects toemployeeMap
, mapping theirempId
as the key and the object as the value. - Add one duplicate
Employee
/Developer
/Designer
object toemployeeMap
.
- Initialize
-
Create the following method in
EmployeeRepository
that returns theMap<Integer, Employee>
:public Map<Integer, Employee> getEmployeeMap()
-
In the
CollectionTest
class, get theEmployee
map by callinggetEmployeeMap()
method. -
Print the total number of employees.
-
Run the
CollectionTest
class and observe that it will print "No of Employees: 6" since we have added one duplicate key.- Note:
Map
doesn't allow duplicate keys but will allow duplicate values.
- Note:
-
Iterate through
employeeMap
and display eachEmployee
profile by callingdisplayProfile(Employee)
on thePayrollImpl
object.