如何在 java 流中使用 thenComparing
How to use thenComparing in java stream
我有一个以字符串作为值的映射。我想先按长度排序,如果字符串的长度相同,我想按字母顺序排序。
我写了那些代码:
String out = outMap.values().stream()
.sorted(Comparator.comparing(e -> e.length()).thenComparing()...)
.collect(Collectors.joining());
问题是,当我写 thenComparing 时,我不能再使用 e.length()
。我该如何解决?
编辑:Map<Character, String>
。我想对字符串进行排序,并通过连接所有字符串在输出中生成一个字符串。
怎么样
String out = outMap.values().stream()
.sorted(Comparator.comparingInt(String::length)
.thenComparing(Comparator.naturalOrder()))
//OR .thenComparing(String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.joining());
我有一个以字符串作为值的映射。我想先按长度排序,如果字符串的长度相同,我想按字母顺序排序。 我写了那些代码:
String out = outMap.values().stream()
.sorted(Comparator.comparing(e -> e.length()).thenComparing()...)
.collect(Collectors.joining());
问题是,当我写 thenComparing 时,我不能再使用 e.length()
。我该如何解决?
编辑:Map<Character, String>
。我想对字符串进行排序,并通过连接所有字符串在输出中生成一个字符串。
怎么样
String out = outMap.values().stream()
.sorted(Comparator.comparingInt(String::length)
.thenComparing(Comparator.naturalOrder()))
//OR .thenComparing(String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.joining());