Comparator.comparingInt(a -> a[1])
时间: 2023-10-21 14:37:28 浏览: 150
This is a method reference in Java that creates a comparator for an array of integers.
The "comparingInt" method is a static method in the Comparator interface that takes a function as its argument. The function maps an array to an integer value, which is used for comparison.
In this case, the lambda expression "a -> a[1]" is the mapping function. It takes an array "a" and returns the integer at the index 1 of the array.
The resulting comparator will compare arrays based on their second element (index 1). It can be used, for example, to sort a list of arrays based on the values in their second column.
相关问题
List<Integer> topKeys = knowledgeProblemMap.entrySet().stream() .sorted(Comparator.comparingInt((Map.Entry<Integer, List<Integer>> entry) -> entry.getValue().size()) .thenComparing((a, b) -> DateUtil.dateAfterCompare(knowledgeCreateDateMap.get(a.getKey()), knowledgeCreateDateMap.get(b.getKey())) ? 1 : 0 ).reversed()) .limit(knowledgeCount) // 取前10个 .map(Map.Entry::getKey) .collect(Collectors.toList());
这段代码的作用是从一个包含知识点和问题列表的 Map 中,按照问题数量和知识点创建时间进行排序,取出前面排名靠前的若干个知识点,并将它们的键值(即知识点的 ID)存储在一个列表中返回。其中:
- `knowledgeProblemMap` 是一个包含知识点和问题列表的 Map;
- `knowledgeCreateDateMap` 是一个包含知识点创建时间的 Map;
- `knowledgeCount` 是需要取出的知识点数量。
java 逆向排序_java – 是否可以使用Comparator.comparingInt链以反向自然顺序对两个字段进行排序...
是的,可以使用`Comparator.comparingInt`链以反向自然顺序对两个字段进行排序。可以使用`reversed()`方法来反转比较器的自然顺序。例如,假设您有一个具有两个整数字段的Java类`Person`,您希望以第一个字段的逆序和第二个字段的自然顺序对它们进行排序。您可以使用以下代码:
```
List<Person> people = ...; // assume you have a list of Person objects
Comparator<Person> comparator = Comparator.comparingInt((Person p) -> p.getFirstField()).reversed()
.thenComparingInt((Person p) -> p.getSecondField());
Collections.sort(people, comparator);
```
这将首先按第一个字段的逆序排序,然后按第二个字段的自然顺序排序。
阅读全文