public <E> List<E> getDuplicateElements(List<E> list) {
HashMap<E, Integer> hs = new HashMap<E, Integer>();
for (E e : list) {
Integer count = 1;
if (hs.get(e) != null) {
count = hs.get(e) + 1;
}
hs.put(e, count);
}
List<E> anwer = new ArrayList<>();
for (E key : hs.keySet()) {
if (hs.get(key) != null & hs.get(key) > 1) {
anwer.add(key);
}
}
return anwer;
}
一个List获取重复值通用方法
最新推荐文章于 2024-04-11 11:23:18 发布