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");
}
}
}