Map<String, Student> storage = new TreeMap<>((o1, o2) -> o2.compareTo(o1))
时间: 2024-04-26 15:26:57 浏览: 69
这是一个使用Java 8 Lambda表达式实现的Map构造函数,它创建了一个新的TreeMap,其中键以逆序排序,并且值是Student类型的对象。具体来说,Lambda表达式 `(o1, o2) -> o2.compareTo(o1)` 表示比较器,它将两个对象作为参数并返回一个int值,表示它们的顺序关系。在这种情况下,它使用 `compareTo` 方法将第二个对象与第一个对象进行比较,以实现逆序排序。因此,这个Map中的键按照逆序排列,并且值是Student类型的对象,可以通过键来访问相应的学生对象。
相关问题
Map<String, List<GetRecruitListOutBO>> sortedMap = new TreeMap<>(Collections.reverseOrder()); 将这个map的key作为新map集合的value,val作为新map的value
可以使用 Java 8 的流式编程和 Lambda 表达式来实现这个功能。可以按照以下步骤操作:
1. 创建一个新的 Map 对象。
2. 使用 forEach() 方法迭代原始的 Map 对象中的每个 Entry 对象。
3. 将原始 Map 中的 Entry 对象的 key 作为新 Map 的 value,将 Entry 对象的 value 作为新 Map 的 key,并将其添加到新的 Map 中。
下面是示例代码:
```
Map<String, List<GetRecruitListOutBO>> sortedMap = new TreeMap<>(Collections.reverseOrder());
// 创建新的 Map 对象
Map<List<GetRecruitListOutBO>, String> newMap = new HashMap<>();
// 迭代原始的 Map 对象,并将其转换为新的 Map 对象
sortedMap.forEach((key, value) -> newMap.put(value, key));
// 输出新的 Map 对象
System.out.println(newMap);
```
这段代码将会把 sortedMap 中的 key 转换为新 Map 的 value,将 sortedMap 中的 value 转换为新 Map 的 key,并将它们添加到新的 Map 中。注意,这里使用了 Collections.reverseOrder() 方法来创建一个反向的 TreeMap 对象,以便按照原始 Map 中的 key 降序排序。
import java.util.*; public class Main { public static void main(String[] args) { TreeMap<String, Student> map = new TreeMap<String, Student>(new MyComparator()); Scanner sc = new Scanner(System.in); while (true) { String ID = sc.next(); if (ID.equals("end")) { break; } String name = sc.next(); int score = sc.nextInt(); Student student = new Student(ID, name, score); map.put(ID, student); } Set<String> keyset = new TreeSet<>(); keyset = map.keySet(); for (String key : keyset) { Student student = map.get(key); System.out.println(student.getID() + " " + student.getName() + " " + student.getScore()); } } } class Student { private String ID;//学号 private String name;//姓名 private int score;//成绩 public Student() { } public Student(String ID, String name, int score) { this.ID = ID; this.name = name; this.score = score; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } } class MyComparator implements Comparator<String> { TreeMap<String, Student> map = new TreeMap<String, Student>(); public MyComparator() { } public MyComparator(TreeMap<String, Student> map) { this.map = map; } @Override public int compare(String o1, String o2) { Student s1 = map.get(o1); Student s2 = map.get(o2); if (s1.getScore() < s2.getScore()) { return 1; } else if (s1.getScore() > s2.getScore()) { return -1; } else { return 0; } } }我想要把根据成绩排序改为根据学号从大到小排序
要按照学号从大到小排序,你可以将 `TreeMap` 的泛型参数中 `String` 改为 `Integer`,这样可以将学号转换为整数类型进行比较。同时,你也需要修改 `MyComparator` 中的 `compare` 方法,将比较逻辑改为根据学号从大到小排序,例如:
```java
class MyComparator implements Comparator<Integer> {
TreeMap<Integer, Student> map;
public MyComparator(TreeMap<Integer, Student> map) {
this.map = map;
}
@Override
public int compare(Integer o1, Integer o2) {
Student s1 = map.get(o1);
Student s2 = map.get(o2);
return o2.compareTo(o1);
}
}
```
在 `compare` 方法中,我们首先获取到要比较的两个学生对象 `s1` 和 `s2`,然后通过比较它们的学号大小来决定它们的顺序。由于要按照学号从大到小排序,因此我们将 `o2` 和 `o1` 进行比较,这样就可以实现根据学号从大到小排序的功能了。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)