在Java中从多个列表中获取所有重复值
我想从多个整数列表中获取所有重复的值。 令人困惑的是,这些整数列表位于类似LinkedHashMap流的映射中 到目前为止,我尝试使用retainAll,但如果具有duplicate的列表彼此不相邻,则我的代码不起作用在Java中从多个列表中获取所有重复值,java,collections,java-5,Java,Collections,Java 5,我想从多个整数列表中获取所有重复的值。 令人困惑的是,这些整数列表位于类似LinkedHashMap流的映射中 到目前为止,我尝试使用retainAll,但如果具有duplicate的列表彼此不相邻,则我的代码不起作用 for (Map.Entry<String,LinkedHashMap<String,List<Integer>>> entry : streams.entrySet()) { String currentStream = entry
for (Map.Entry<String,LinkedHashMap<String,List<Integer>>> entry : streams.entrySet()) {
String currentStream = entry.getKey();
LinkedHashMap<String,List<Integer>> bDescList = entry.getValue();
for (Map.Entry<String,List<Integer>> bDesc : bDescList.entrySet()) {
if (firstIteration) {
prevBDesc = bDesc;
firstIteration = false;
} else {
List<Integer> currentList = prevBDesc.getValue();
List<Integer> nextList = bDesc.getValue();
duplicates = new ArrayList<Integer>(currentList);
duplicates.retainAll(nextList);
allDuplicates.addAll(duplicates); //Set<Integer>
prevBDesc = bDesc;
}
}
}
for(Map.Entry:streams.entrySet()){
字符串currentStream=entry.getKey();
LinkedHashMap bdesList=entry.getValue();
对于(Map.Entry bDesc:bdeslist.entrySet()){
if(第一次迭代){
prevBDesc=bDesc;
第一次迭代=假;
}否则{
List currentList=prevBDesc.getValue();
List nextList=bDesc.getValue();
duplicates=新的ArrayList(当前列表);
副本。保留(下一个列表);
allDuplicates.addAll(duplicates);//设置
prevBDesc=bDesc;
}
}
}
编辑:
抱歉,伙计们,我忘了添加它正在Java 1.5上运行。Edit
这假定您正在查找任何重复的值。这包括在同一列表中查找重复项。如果我误解了这个问题,请纠正我
您可以在O(N)时间和O(N)空间中执行此操作,方法是在计算每个整数的出现次数时遍历嵌套哈希。然后,我们可以过滤到出现多次的整数
Map<String, List<Integer>> innerMap = new HashMap<>();
innerMap.put("bDesc_1000", Arrays.asList(62, 72, 82, 92, 102, 112, 122));
innerMap.put("bDesc_1001", Arrays.asList(180, 190, 200, 210, 220, 230, 240));
innerMap.put("cMessage_1000", Collections.singletonList(112));
innerMap.put("cMessage_1001", Collections.singletonList(232));
Map<String, Map<String, List<Integer>>> map = new HashMap<>();
map.put("break_desc100", innerMap);
Map<Integer, Integer> occurrenceMap = new HashMap<>();
map.forEach((outerKey, outerValue) -> {
outerValue.forEach((innerKey, innerValue) -> {
innerValue.forEach((element -> occurrenceMap.merge(element, 1, Integer::sum)
));
});
});
List<Integer> duplicates = occurrenceMap.entrySet().stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
System.out.println(duplicates);
这似乎是一个适合流的任务:
Map<Integer, Long> counts = streams.values().stream()
.flatMap(bDescList -> bDescList.values().stream())
.flatMap(nextList -> nextList.stream())
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()));
counts.values().removeIf(c -> c == 1L);
Set<Integer> duplicates = counts.keySet();
Map counts=streams.values().stream()
.flatMap(bdeslist->bdeslist.values().stream())
.flatMap(nextList->nextList.stream())
.collect(收集器.groupingBy(
Function.identity(),
收集器。计数();
counts.values().removeIf(c->c==1L);
Set duplicates=counts.keySet();
此代码首先创建计数的映射。为此,它首先流化外部贴图的值,然后使用Stream.flatMap
创建一个包含所有内部贴图值的新流。由于这些值实际上是列表,我们需要再次使用Stream.flatMap
,以最终获得整数的流。(我已经从你的问题中保留了变量名)
我们收集到一个计数图,其中键是所有内部映射列表值中的数字,这些值是这些数字中每一个的计数,包括所有映射和列表
然后,我们从计数映射中删除值为1
的所有条目。其余的键是重复的数字
编辑:以下是Java 5中的等效代码。。。如果一个元素在一个列表中出现多次,或者一个元素在多个列表中出现多次,则将该元素计为重复元素?如果该元素在任何列表中至少出现两次,则我应该能够获得该数字。抱歉,我忘了添加它在Java 1.5上运行的内容。抱歉,我忘了添加它在Java 1.5上运行的内容
[112]
Map<Integer, Long> counts = streams.values().stream()
.flatMap(bDescList -> bDescList.values().stream())
.flatMap(nextList -> nextList.stream())
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()));
counts.values().removeIf(c -> c == 1L);
Set<Integer> duplicates = counts.keySet();