[英]Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings
Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings? 使用Java 8,创建排序和分组的字符串列表的最简洁方法是什么? Show the old way and the new way using Lambdas and the Collections and Streams framework. 使用Lambdas和Collections and Streams框架显示旧方法和新方法。
You can show using 3rd party libraries (popular ones) for the old (or new) way. 您可以使用旧的(或新的)方式使用第三方库(流行的)。
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. 甲Map
将产生的含第一个字母作为键。 The values in the map will be a sorted List
of all the words beginning with that key (letter): 地图中的值将是以该键(字母)开头的所有单词的排序List
:
Using Java, with no help from 3rd party libraries, there is the old way and the new way. 使用Java,没有第三方库的帮助,有旧方法和新方法。 Just sorting used to be easy with Collections.sort(..). 只需使用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.
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);
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);
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);
With the popular third-party Guava library, 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.