gpt4 book ai didi

java流排序与字符串比较器忽略大小写

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:40 24 4
gpt4 key购买 nike

<分区>

hotels.stream().sorted(Comparator.comparing(Hotel::getCity)
.thenComparing(hotel -> hotel.getName().toUpperCase())))
.collect(Collectors.toList());

请问有没有更简洁的写法.thenComparing(hotel -> hotel.getName().toUpperCase()),我找到了一个String.CASE_INSENSITIVE_ORDER 但如何在 hotel.getName() 上使用此比较器。

更新:应用了@Arnaud Denoyelle 的建议。

hotels.stream().sorted(Comparator.comparing(Hotel::getCity)
.thenComparing(Hotel::getName, String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.toList());

看起来更好。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com