comparing(Article::getCreatedate).reversed().
时间: 2024-04-04 16:35:19 浏览: 13
这段代码使用Java 8中的Comparator的方法,对Article对象的创建日期进行比较,并按照创建日期倒序排列。具体来说,它将Article对象作为参数,使用Article::getCreatedate方法获取创建日期,然后使用reversed()方法将比较结果反转,以实现倒序排列。此方法可以用于对Article对象的列表进行排序,以便按照创建日期的倒序显示文章。
相关问题
result.sort(Comparator.comparing(UserMissionDto::getMIndex).reversed().thenComparing(UserMissionDto::getStatus).reversed()); 这行代码什么意思
这行代码是对一个 `List<UserMissionDto>` 的对象进行排序。根据 `UserMissionDto` 对象的 `mIndex` 属性进行降序排序,如果 `mIndex` 相同,则根据 `status` 属性进行降序排序。这里使用了 Java 8 中的 Comparator 接口和方法引用来实现排序逻辑。
list2 = list.stream().sorted(Comparator.comparing(dto -> orderList.indexOf(dto.getOrgType())) .thenComparing(CxCadreSituationDTO::getEstab,Comparator.nullsFirst(String::compareTo)).reversed()) .map(CxCadreSituationDTO::getEstabDesc).distinct().collect(Collectors.toList());getOrgType报错Cannot resolve method 'getOrgType()'
这个报错说明 `dto` 对象没有 `getOrgType()` 方法。你可以尝试将 `Comparator.comparing(dto -> orderList.indexOf(dto.getOrgType()))` 这一行改为 `Comparator.comparing(CxCadreSituationDTO::getOrgType, Comparator.comparingInt(orderList::indexOf))`,这样就可以直接使用 `getOrgType()` 方法了。即:
```
list2 = list.stream()
.sorted(Comparator.comparing(CxCadreSituationDTO::getOrgType, Comparator.comparingInt(orderList::indexOf))
.thenComparing(CxCadreSituationDTO::getEstab, Comparator.nullsFirst(String::compareTo)).reversed())
.map(CxCadreSituationDTO::getEstabDesc)
.distinct()
.collect(Collectors.toList());
```
这里使用了方法引用 `CxCadreSituationDTO::getOrgType` 来获取 `orgType` 属性。同时使用了 `Comparator.comparingInt(orderList::indexOf))` 来获取 `orderList` 中元素的索引,这个方法会返回一个 `int` 类型的索引值,可以直接用于比较。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)