Map<Long, Employee.status> prevStatus = empRecords.stream()
.collect(Collectors.toMap(employeeRecord::getEmloyeeID,
employeeRecord::getEmployeeStatus));
I already have the above code, I need to add a similar operation but instead of creating a new Map I want to add the result to the existing map.
prevStatus = empRecords.stream()
.collect(Collectors.**toMap**(employeeRecord::getEmloyeeID,
employeeRecord::**getUSEmployeeStatus**));
CodePudding user response:
You can create a new Map
and add its entries to the existing Map
:
prevStatus.putAll(
empRecords.stream()
.collect(Collectors.toMap(employeeRecord::getEmloyeeID,
employeeRecord::getUSEmployeeStatus)));
Or you can use forEach
instead of collect
:
empRecords.stream()
.forEach(emp -> prevStatus.put(emp.getEmloyeeID (),
emp.getEmployeeStatus()));
CodePudding user response:
This is expected code
Map<Long, Employee.status> prevStatus = empRecords.stream()
.collect(Collectors.toMap(employeeRecord::getEmloyeeID,
employeeRecord::getEmployeeStatus,(k,v)->k,()->existingMap));
Where (k,v)-> k
logic is for what if key is already present in existing Map and
existingMap
isyour existing map