<分区>
谁知道在 String ArrayList 中查找重复项并打印出重复项的最有效方法?
例如,我有一个包含以下内容的 ArrayList:
ArrayList<String> carLIST = new ArrayList<String>();
carLIST = {"Car1", "Car2", "Car3", "Car1", "Car2", "Car2"};
基本上列表中的任何内容我都不止一次地寻找重复项(我想我已经在下面完成了)并且还返回了 System.out.println();显示以下内容:
Car1 : count=2
Car2 : count=3
Map<String,Integer> repeatationMap = new HashMap<String,Integer>();
for(String str : carLIST) {
if (repeatationMap.containsKey(str) {
repeatationMap.put(str,repeatationMap.get(str) +1);
}
else {
epeatationMap.put(str, 1);
}
// if (repeatationMap.get(str) > 1) {
// System.out.println(repeatationMap.entrySet());
// }
}
注释掉的代码是我认为打印出重复项的代码,但我大错特错了!不知道如何打印出列表中重复的汽车并显示其数量。