如何在Java流中使用thenComparing [英] How to use thenComparing in java stream
本文介绍了如何在Java流中使用thenComparing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以字符串为值的映射.我想首先按长度排序,如果字符串的长度相同,我想按字母排序. 我写了那些代码:
I have a map with strings as values. I want to sort it firstly by length, and if length of the strings is the same, i want to sort it alphabetic. I wrote those code :
String out = outMap.values().stream()
.sorted(Comparator.comparing(e -> e.length()).thenComparing()...)
.collect(Collectors.joining());
问题是,当我写thenComparing时,我不能再使用e.length()
.我该如何解决?
The problem is, when i am writing thenComparing, I couldn't use e.length()
anymore. How can i fix it?
Map<Character, String>
.我想对所有字符串进行排序,并通过输出使所有字符串成为一个字符串.
EDIT : Map<Character, String>
. I want to sort the strings and make one string in output by concat all of them.
推荐答案
如何
String out = outMap.values().stream()
.sorted(Comparator.comparingInt(String::length)
.thenComparing(Comparator.naturalOrder()))
//OR .thenComparing(String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.joining());
这篇关于如何在Java流中使用thenComparing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文