debtBuyerByMainBuyerId.stream() .filter(map -> !Objects.equals(MapUtils.getInteger(map, "buyerId"), mainBuyerId)) .sorted(Comparator.comparing(map -> (BigInteger) MapUtils.getObject(map, "maxDebtDay"))) 实现倒序或正序
时间: 2024-02-22 16:54:50 浏览: 46
要实现倒序或正序,可以使用`reversed()`方法或`Comparator.reverseOrder()`静态方法。例如,要按照最大欠款天数从大到小排序,可以这样写:
```
debtBuyerByMainBuyerId.stream()
.filter(map -> !Objects.equals(MapUtils.getInteger(map, "buyerId"), mainBuyerId))
.sorted(Comparator.comparing(map -> (BigInteger) MapUtils.getObject(map, "maxDebtDay")).reversed())
```
或者这样写:
```
debtBuyerByMainBuyerId.stream()
.filter(map -> !Objects.equals(MapUtils.getInteger(map, "buyerId"), mainBuyerId))
.sorted(Comparator.comparing(map -> (BigInteger) MapUtils.getObject(map, "maxDebtDay"), Comparator.reverseOrder()))
```
这两种写法都可以将排序结果倒序输出。如果要正序排序,可以将`reversed()`方法或`Comparator.reverseOrder()`静态方法去掉即可。
相关问题
filter(str -> !"".equals(str))
这个问题是一个Java 8中的Stream API的用法,它的作用是将一个字符串列表中的空字符串过滤掉。具体实现方式是使用filter方法,传入一个Lambda表达式,该表达式接受一个字符串参数,返回一个布尔值,表示该字符串是否应该被保留。在这个例子中,Lambda表达式是`str -> !"".equals(str)`,表示只保留非空字符串。
给我解释一下 questionVoList.removeIf(questionVo -> !Objects.equals(questionVo.getQuestionType(), type));
这行代码是使用Lambda表达式和Stream API来移除列表中不满足特定条件的元素。让我逐步解释一下:
1. `questionVoList` 是一个列表(List)对象,其中包含了一系列的 `questionVo` 元素。
2. `removeIf()` 是 `List` 接口提供的方法,它接受一个 `Predicate` 参数,用于判断某个元素是否需要被移除。
3. `questionVo -> !Objects.equals(questionVo.getQuestionType(), type)` 是一个 Lambda 表达式,表示对于列表中的每个 `questionVo` 元素,判断其 `questionType` 是否与给定的 `type` 相等。如果不相等,则返回 `true`,表示需要移除该元素;否则返回 `false`,表示保留该元素。
4. 因此,`removeIf()` 方法会遍历列表中的每个元素,并根据 Lambda 表达式的返回值来决定是否移除该元素。
总结起来,这行代码的作用是从 `questionVoList` 中移除所有 `questionType` 不等于给定 `type` 的元素。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)