Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 1.65 KB

File metadata and controls

59 lines (45 loc) · 1.65 KB

Data Set

   List<Footballer> getFootballers() {
        return List.of(
                new Footballer("Messi", 32, Gender.MALE, List.of("CF","CAM", "RF")),
                new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")),
                new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")),
                new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")),
                new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")),
                new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")),
                new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")),
                new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF"))
        );
    }

FlatMap:

A stream can hold complex data structures like Stream<List>. In cases like this, flatMap() helps us to flatten the data structure to simplify further operations.

Example: Get all positions of male footballers less than 30 years old.

        String allPositionsOfMaleLessThan30y = footballerList.stream()
        .filter(footballer -> footballer.getGender().equals(Gender.MALE))
        .filter(footballer -> footballer.getAge() < 30)
        .map(Footballer::getPositions)
        .flatMap(Collection::stream)
        .collect(Collectors.joining(","));

Output:

allPositionsOfMaleLessThan30y = CF,CAM,LF,CM,CAM,GK,CM,CDM

Previous: B Map

Next: D Distinct

[Distinct](./D_Distinct.md) ->