javajava-stream

Java streams for adding to a list if it exists or creating a new one in a HashMap


I have a list of strings that must be split using a delimiter and then get the second value of the split to create a map of string arrays.

Here is an example:

A list of strings like the following:

["item1:parent1", "item2:parent1", "item3:parent2"]

Should be converted to a map that has the following entries:

<key: "parent1", value: ["item1", "item2"]>,
<key: "parent2", value: ["item3"]>

I tried to follow the solution given in this question but with no success

Sample code:

var x = new ArrayList<>(List.of("item1:parent1", "item2:parent1", "item3:parent2"));
var res = x.stream().map(s->s.split(":")).collect(Collectors.groupingBy(???));

Solution

  • Assuming the splited array has always a length of 2, something like below should work

    var list = List.of("item1:parent1", "item2:parent1", "item3:parent2");
    var map = list.stream()
                .map(s -> s.split(":"))
                .collect(Collectors.groupingBy(
                        s -> s[1], 
                        Collectors.mapping(s -> s[0],  Collectors.toList())));
        
    System.out.println(map);