import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String, Integer>map = new HashMap();
while (in.hasNext()) {
String str = in.next();
// System.out.println("读取到" + str);
if (map.containsKey(str)) {
map.put(str, map.get(str) + 1);
} else {
map.put(str, 1);
}
}
List<Map.Entry<String, Integer>>entityList = new ArrayList(map.entrySet());
Collections.sort(entityList, new Comparator<Map.Entry<String, Integer>> () {
@Override
public int compare(Map.Entry<String, Integer> e1,
Map.Entry<String, Integer> e2) {
if (e1.getValue() == e2.getValue()) {
return e1.getKey().compareTo(e2.getKey());
} else {
return e2.getValue() - e1.getValue();
}
}
});
for (Map.Entry<String, Integer>ans : entityList) {
if (ans.getValue() >= 3) {
System.out.println(ans.getKey());
}
}
}
}
记录一下正确代码,准备用Java刷算法了,一道简单的哈希题

京公网安备 11010502036488号