- Joined
- Sep 16, 2022
- Messages
- 1
- Reaction score
- 0
import java.util.HashMap;Here is my code so far I was able to merge three maps but I need to use an optional counter from a pojo.
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class mapMerge {
public static void main(String[] args) {
Map<String, Integer> branch1 = new HashMap<>();
branch1.put("Marvin", 3);
branch1.put("Aldrin", 2);
branch1.put("John", 5);
branch1.put("Roland", 4);
Map<String, Integer> branch2 = new HashMap<>();
branch2.put("Marvin", 9);
branch2.put("Roland", 8);
Map<String, Integer> branch3 = new HashMap<>();
branch3.put("John", 3);
branch3.put("Roland", 4);
Map<String, Integer> result = Stream.of(branch1, branch2, branch3).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> value1 + value2));
System.out.println(result);
}
}
import java.util.Optional;Here is the pojo;
public class UserStats {
Long visitCount = null;
Optional<Long> getVisitCount() {
return Optional.ofNullable(this.visitCount);
}
void setVisitCount(Long visitCount) {
this.visitCount = visitCount;
}
}
And here is the function I need to use;
import java.util.Map;
class VisitCounter {
Map<Long, Long> count(Map<String, UserStats>... visits) {
return null;
}
}
This is the main objective:
Implement Map<Long, Long> count(Map<String, UserStats>... visits) of the VisitCounter class
All branches of the company stores records of employee visitors in a map of employeeNumber mapping
to UserStats. The employeeNumber is in String form and UserStats is a User Defined Class object that
returns Optional<Long> for the visitCount. The management provided the above interface where all the
maps from all branches are submitted and processed every month. The method returns a map of
employeeNumber (now as Long) mapped to the their total count of visits to all branches during the
month