简体   繁体   English

使用Java 8,创建排序和分组字符串列表的最简洁方法是什么

[英]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

  • key: A values: ["Ananas","Apple"] 关键:A值:[“Ananas”,“Apple”]
  • key: B values: ["Banana","Beer","Blue Berry"] 关键:B值:[“香蕉”,“啤酒”,“蓝莓”]
  • key: M values: ["Mango"] 关键:M值:[“芒果”]

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.

Old Java 老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);

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.

相关问题 使用Java 8,打印文件中所有行的最优选和简洁方法是什么? - Using Java 8, what is the most preferred and concise way of printing all the lines in a file? 使用Java 8,迭代地图中所有条目的最简洁方法是什么? - Using Java 8, what is the most concise way of iterating through all the entries in a map? Java中最简洁的方法是从“AlphaSuffix”中获取“Alpha”? - What's the most concise way in Java to get the “Alpha” out of “AlphaSuffix”? 编写此 java 代码的最简洁/最佳方式是什么? - What's the most concise / best way to write this java code? 将字符串插入已排序的数组字符串列表中的最有效方法是什么? - What's the most efficient way to insert a string into an already-sorted array list of strings? 在 Java 中比较版本的最简洁方法 - Most concise way of comparing versions in Java 用反序列化编写Java类以表示简单的JSON文档的最简洁方法是什么? - What is the most concise way to write a Java class to represent a simple JSON document, with deserialization? 使用Java确定一个月内天数的最简洁方法(使用布尔运算符) - Most Concise Way to Determine Number of Days in a Month with Java (Using boolean Operators) 在Java中测试函数输出的简洁方法是什么? - What is a concise way to test outputs of a function in java? 在Scala中构造/构建JavaBean对象的最简洁方法是什么? - What is the most concise way to construct/build JavaBean objects in Scala?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM