某公司现需要统计员工出勤次数,具体要求如下: 输入样例: Mark Tom Ivor Mark Ivor Mark Jack end 输入样例解释: 每行表示某天出勤的员工名单,以空格间隔。 end表示输入结束 输出样例: Mark 3 Ivor 2 Tom 1 Jack 1 输出样例解释: 按出勤天数倒序输出,若出勤次数相同则按输入顺序输出(即先输入的先输出,样例中Tom比Jack先输入,因此先输出Tom)。每名员工占一行,格式为员工名+空格+出勤次数,给出Java代码实现
时间: 2024-03-22 10:39:36 浏览: 190
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, Integer> map = new HashMap<>();
String line;
while (sc.hasNextLine()) {
line = sc.nextLine();
if (line.equals("end")) break;
String[] employees = line.split(" ");
for (String employee : employees) {
map.put(employee, map.getOrDefault(employee, 0) + 1);
}
}
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((o1, o2) -> {
if (o1.getValue().equals(o2.getValue())) {
return 0;
} else {
return o2.getValue().compareTo(o1.getValue());
}
});
for (Map.Entry<String, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
```
阅读全文