我正在使用Java。我有一张地图如下所示:
List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();我正在将带有一些键值对的Map对象插入到上面的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);现在,我想迭代
listMap(`List<Map<String, String>> listMap`) 然后查找映射中的键jobDescription是否有任何重复/相同的值,然后检查interviewType键值的值,并查看该值出现的次数。
在上面的示例中,键jobDescription的值在所有映射对象(即Java Developer-SpringBoot)中都是相同的。然后验证键interviewType的值,并查看每个值的出现次数(在上述情况下,L2重复两次,L1重复一次)。最后,我需要再构造一个包含我的观察结果的Map。
例如(为了说明起见,描述了这些数据,但是这实际上应该进入一个新的Map中)。
"jobDescription" - "Count of L2" - "Count of L1"
-------------------------------------------------------------------
"Java Developer-SpringBoot" 2 1有人能帮我吗?
下面给出了我正在尝试的代码:
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");
}
}
}发布于 2021-09-26 11:32:09
使用map存储数据看起来很奇怪,如下所示:
所有的值都被限制在相同的type(String).
对类Job进行建模是一种更合适的方法。然后遵循@Joe的建议,Group by multiple field names in java 8
下面的程序将输出
Java开发人员-SpringBootL1: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;
}
}
}发布于 2021-09-26 11:12:48
流在列表和筛选映射上,其中包含一个带有键的条目:jobDescription和value:Java Developer-SpringBoot,平面映射以获取所有映射的所有条目,过滤器条目以interviewType作为键,将每个条目映射到其值,收集到使用Function.identity()映射并映射到频率:
Map<String,Long> result =
listMap.stream()
.filter(m -> m.entrySet().contains(Map.entry("jobDescription","Java Developer-SpringBoot")))
.flatMap(m -> m.entrySet().stream())
.filter(e -> e.getKey().equals("interviewType"))
.map(Map.Entry::getValue)
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(result);
//output: {L1=1, L2=2}https://stackoverflow.com/questions/69334041
复制相似问题