3

Lets say I have following an class named Employee and a List of these Employee-classes:

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"));

Is it possible to convert it to following Map with Java Stream-API?

{A.B.C=[Cristiano Ronaldo, Michael Phelps], D.E.F=[Aydin Korkmaz, Diego Armando Maradona, Usain Bolton]} 
0

2 Answers 2

6
  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())
Sign up to request clarification or add additional context in comments.

6 Comments

Stream.of(x.getPraefix(), x.getMiddleFix(), x.getPostfix()) .collect(Collectors.joining(".") could be replaced with simple concatenation using +
@Ivan ooh my! thx, this is what happens when I try to do two things at a time.
That leaves out the "." delimiter though. I think the first one was better.
@Vasan neither :) how about a 3-rd option (that I actually would use myself)
Ah yes, I'd forgot about StringJoiner. +1
|
1

You could also use the toMap collector:

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;}));

2 Comments

this is exactly what I wanted to have today in default jdk, ArrayList.of(...), or HashSet.of(...), would make this a little nicer
@Eugene couldn't agree more, it's definitely long-winded with the current approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.