I am using Java. I have a Map as shown below :
List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
I am inserting Map objects with some key value pairs into the above List<Map<String, String>> listMap
.
Map<String, String> map1 = new HashMap<String, String>(); map1.put("jobDescription", "Java Developer-SpringBoot"); map1.put("interviewType", "L2"); map1.put("hired", "yes"); listMap.add(map1); Map<String, String> map2 = new HashMap<String, String>(); map2.put("jobDescription", "Java Developer-SpringBoot"); map2.put("interviewType", "L2"); map2.put("hired", "yes"); listMap.add(map2); Map<String, String> map3 = new HashMap<String, String>(); map3.put("jobDescription", "Java Developer-SpringBoot"); map3.put("interviewType", "L1"); map3.put("hired", "no"); listMap.add(map3);
Now, I want to iterate
listMap(`List<Map<String, String>> listMap`)
and then find if there are any duplicate/same values for the key jobDescription
in any of the map, then check for the value of interviewType
key’s value and see the number of occurrences of the value.
In the above example, the values for the key jobDescription
is same in all the Map objects(i.e.Java Developer-SpringBoot
). Then verify the values for the key interviewType
and see the number of occurrences of each value(In the above case L2
repeated twice and L1
once). Finally I need to construct one more Map
that contains my observations.
For example(This data is depicted for illustration purpose, but this should actually go into a new Map
:
"jobDescription" - "Count of L2" - "Count of L1" ------------------------------------------------------------------- "Java Developer-SpringBoot" 2 1
Can anyone help me on this?
The code that I am trying is given below:
package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class Sample { public static void main(String[] args) { List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("jobDescription", "Java Developer-SpringBoot"); map1.put("interviewType", "L2"); map1.put("hired", "yes"); listMap.add(map1); Map<String, String> map2 = new HashMap<String, String>(); map2.put("jobDescription", "Java Developer-SpringBoot"); map2.put("interviewType", "L2"); map2.put("hired", "yes"); listMap.add(map2); Map<String, String> map3 = new HashMap<String, String>(); map3.put("jobDescription", "Java Developer-SpringBoot"); map3.put("interviewType", "L1"); map3.put("hired", "no"); listMap.add(map3); Map<String, Map<String, String>> requiredMap = new HashMap<String, Map<String, String>>(); for (Map<String, String> someMap : listMap) { int count = Collections.frequency(someMap.values(), "L2"); } } }
Advertisement
Answer
It looks strange to use map to store the data, as:
- All values are limited to the same type(String).
- There is no limitation on the keys.
Modelling a class Job
is a more proper way. Then follow @Joe comment suggestion, Group by multiple field names in java 8
Below program will output
Java Developer-SpringBoot L1:1, L2:2
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Sample { public static void main(String[] args) { List<Job> jobs = new ArrayList<>(); jobs.add(new Job("Java Developer-SpringBoot", "L2", "yes")); jobs.add(new Job("Java Developer-SpringBoot", "L2", "yes")); jobs.add(new Job("Java Developer-SpringBoot", "L1", "no")); Map<String, Map<String, Long>> jobDescriptionToInterviewTypeToCountMapMap = jobs.stream().collect(Collectors.groupingBy(Job::getJobDescription, Collectors.groupingBy(Job::getInterviewType, Collectors.counting()))); for (Map.Entry<String, Map<String, Long>> entry : jobDescriptionToInterviewTypeToCountMapMap.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue().entrySet().stream().map((e) -> e.getKey() + ":" + e.getValue()).collect(Collectors.joining(", "))); } } public static class Job { public Job(String jobDescription, String interviewType, String hired) { this.jobDescription = jobDescription; this.interviewType = interviewType; this.hired = hired; } private String jobDescription; private String interviewType; private String hired; public String getJobDescription() { return jobDescription; } public void setJobDescription(String jobDescription) { this.jobDescription = jobDescription; } public String getInterviewType() { return interviewType; } public void setInterviewType(String interviewType) { this.interviewType = interviewType; } public String getHired() { return hired; } public void setHired(String hired) { this.hired = hired; } } }