使用Collections Util frequency Method找到集合列表内的重复值。
请在下面找到工作代码。
List<Long> nonDuplicateList = new ArrayList<Long>();
nonDuplicateList.add(101L);
nonDuplicateList.add(102L);
nonDuplicateList.add(102L);
nonDuplicateList.add(103L);
nonDuplicateList.add(103L);
nonDuplicateList.add(103L);
nonDuplicateList.add(104L);
nonDuplicateList.add(104L);
nonDuplicateList.add(104L);
System.out.println("Original list " + nonDuplicateList);
List<Long> repeatedList = new ArrayList<Long>();
for (Long longValue : nonDuplicateList) {
if (Collections.frequency(nonDuplicateList, longValue) > 2) {
if (!repeatedList.contains(longValue)) {
repeatedList.add(longValue);
}
}
}
System.out.println("Duplicated List " + repeatedList);
输出是
Original list[101, 102, 102, 103, 103, 103, 104, 104, 104]
Duplicated List [103, 104]
而你是什么意思 “我想103和104”?你想在列表中返回它们?你想打印它们?如果你尝试重新格式化你的问题,这也会很好;) – Mark
我想返回一个列表 – prasad
请分享你现在的代码.. – Sanghita