Java流API:如何转换为集合映射

Java流API:如何转换为集合映射,java,arraylist,hashmap,java-stream,Java,Arraylist,Hashmap,Java Stream,假设我有一个名为Employee的类,以及这些Employee类的列表: class Employee { private String praefix; private String middleFix; private String postfix; private String name; public Employee(String praefix, String middleFix, String postfix, String name) { this.praefix =

假设我有一个名为
Employee
的类,以及这些
Employee
类的列表:

class Employee {

private String praefix;
private String middleFix;
private String postfix;
private String name;

public Employee(String praefix, String middleFix, String postfix, String name) {
    this.praefix = praefix;
    this.middleFix = middleFix;
    this.postfix = postfix;
    this.name = name;
}

public String getPraefix() {
    return praefix;
}

public void setPraefix(String praefix) {
    this.praefix = praefix;
}

public String getMiddleFix() {
    return middleFix;
}

public void setMiddleFix(String middleFix) {
    this.middleFix = middleFix;
}

public String getPostfix() {
    return postfix;
}

public void setPostfix(String postfix) {
    this.postfix = postfix;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}



List<Employee> employees = new ArrayList<>();
employees.add(new Employee("A", "B", "C", "Michael Phelps"));
employees.add(new Employee("A", "B", "C", "Cristiano Ronaldo"));
employees.add(new Employee("D", "E", "F", "Usain Bolton"));
employees.add(new Employee("D", "E", "F", "Diego Armando Maradona"));
employees.add(new Employee("D", "E", "F", "Lionel Messi"));
Map result=employees.stream()
.collect(收集器.groupingBy(
x->String.join(“.”,
x、 getPraefix(),
x、 getMiddleFix(),
x、 getPostfix()),
Collectors.mapping(Employee::getName,Collectors.toList())

您也可以使用
toMap
收集器:

Map<String, List<String>> resultSet = employees.stream()
             .collect(toMap(e ->
               String.join(".", e.getPraefix(), e.getMiddleFix(), e.getPostfix()),
               v -> new ArrayList<>(Collections.singletonList(v.getName())),
               (left, right) -> {left.addAll(right); return left;}));
Map resultSet=employees.stream()
.收集(toMap)(e->
String.join(“.”,e.getPraefix(),e.getMiddleFix(),e.getPostfix()),
v->new ArrayList(Collections.singletonList(v.getName()),
(左,右)->{left.addAll(右);返回左;});

Stream.of(x.getPraefix(),x.getMiddleFix(),x.getPostfix()).collect(collector.joining(“.”)可以用使用
+
@Ivan ooh my!thx的简单连接来代替,这就是当我试图一次做两件事时发生的情况。这样就省去了“.”不过是分隔符。我认为第一个更好。@Vasan note:)第三个选项(我实际上会使用自己)怎么样啊,是的,我忘记了StringJoiner。+1这正是我今天在默认jdk、
ArrayList.of(…)
HashSet.of(…)中想要的
,将使这有点nicer@Eugene非常同意,目前的方法肯定是冗长的。
  Map<String, List<String>> result = employees.stream()
            .collect(Collectors.groupingBy(
                    x -> String.join(".",
                            x.getPraefix(),
                            x.getMiddleFix(),
                            x.getPostfix()),
                    Collectors.mapping(Employee::getName, Collectors.toList())
Map<String, List<String>> resultSet = employees.stream()
             .collect(toMap(e ->
               String.join(".", e.getPraefix(), e.getMiddleFix(), e.getPostfix()),
               v -> new ArrayList<>(Collections.singletonList(v.getName())),
               (left, right) -> {left.addAll(right); return left;}));