使用 Java 8,创建排序和分组的字符串列表的最简洁方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19635179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings
提问by The Coordinator
Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings? Show the old way and the new way using Lambdas and the Collections and Streams framework.
使用 Java 8,创建排序和分组的字符串列表的最简洁方法是什么?展示使用 Lambdas 和 Collections and Streams 框架的旧方法和新方法。
You can show using 3rd party libraries (popular ones) for the old (or new) way.
您可以使用 3rd 方库(流行库)以旧(或新)方式展示。
However, I suggest that vanilla Java be used because that shows the changes that the language changes in Java 8 bring to the table for the task.
但是,我建议使用 vanilla Java,因为这显示了 Java 8 中的语言更改为任务带来的变化。
Input: List<String>
Output: Map<Character<List<String>>
The key of map is 'A' to 'Z'
Each list in the map are sorted.
It will be sorted and grouped such that ...
它将被排序和分组,以便...
Given this list: "Beer", "Apple", "Banana", "Ananas", "Mango", "Blue Berry"
鉴于此列表:“啤酒”、“苹果”、“香蕉”、“香蕉”、“芒果”、“蓝莓”
A Map
will produced containing the first letter as the key. The values in the map will be a sorted List
of all the words beginning with that key (letter):
阿Map
将产生的含第一个字母作为键。映射中的值将对List
以该键(字母)开头的所有单词进行排序:
- key: A values: ["Ananas","Apple"]
- key: B values: ["Banana","Beer","Blue Berry"]
- key: M values: ["Mango"]
- 键:A 值:["Ananas","Apple"]
- 键:B 值:[“香蕉”、“啤酒”、“蓝莓”]
- 键:M 值:["芒果"]
采纳答案by The Coordinator
Using Java, with no help from 3rd party libraries, there is the old way and the new way. Just sorting used to be easy with Collections.sort(..).
使用 Java,没有 3rd 方库的帮助,有旧方法和新方法。使用 Collections.sort(..) 过去只是排序很容易。
The challenge with the old way was that a lot of code was required to group the values.
旧方法的挑战是需要大量代码来对值进行分组。
- Input: List<String>
- Output: Map<Character,<List<String>>
- The key of map is 'A' to 'Z'
- Each list in the map are sorted.
Old Java
旧爪哇
List<String> keywords = Arrays.asList("Apple", "Ananas", "Mango", "Banana", "Beer");
Map<Character, List<String>> result = new HashMap<Character, List<String>>();
for(String k : keywords) {
char firstChar = k.charAt(0);
if(!result.containsKey(firstChar)) {
result.put(firstChar, new ArrayList<String>());
}
result.get(firstChar).add(k);
}
for(List<String> list : result.values()) {
Collections.sort(list);
}
System.out.println(result);
New Java 8
新的 Java 8
List<String> keywords = Arrays.asList("Apple", "Ananas", "Mango", "Banana", "Beer");
Map<Character, List<String>> result = keywords.stream()
.sorted()
.collect(Collectors.groupingBy(it -> it.charAt(0)));
System.out.println(result);
New Java 8 with source data already as a 'Stream'
源数据已作为“流”的新 Java 8
As suggested by @KevinO
正如@KevinO 所建议的
Map<Character, List<String>> result = Stream
.of( "Apple", "Ananas", "Mango", "Banana","Beer")
.sorted()
.collect(Collectors.groupingBy(it -> it.charAt(0)))
System.out.println(result);
回答by Louis Wasserman
With the popular third-party Guavalibrary, compatible with Java 6:
搭配流行的第三方Guava库,兼容 Java 6:
TreeMultimap<Character, String> multimap = TreeMultimap.create();
for (String string : list) {
multimap.put(string.charAt(0), string);
}
return Multimaps.asMap(ImmutableListMultimap.copyOf(multimap));
This does deduplicate strings, so an alternate version that allows duplicate strings:
这确实消除了重复字符串,因此允许重复字符串的替代版本:
ImmutableListMultimap.Builder<Character, String> builder =
ImmutableListMultimap.builder();
for (String string : Ordering.natural().sortedCopy(list)) {
builder.put(string.charAt(0), string);
}
return Multimaps.asMap(builder.build());