Java 8 uses forky streams

I'm curious if this is so possible to not always go straight into the flow. Imagine this example code:

List<Employee> employees = [...];

employees.stream()
    .map(Employee::getCity)
    .map(City::getCountry)
    .map(countryService::determineHazardLevel)
    .filter(level -> level > 5) // integer
    // this line not possible anymore as I drifted away from my employees ...
    .forEach(employee -> employee.setPriority(HIGH))

      

I am going through the list of employees and want them to set a priority flag if they live in a city that is in a country with a certain hazard level of 5 or higher.

I would like to follow the path as described above, but cannot find a way to do it. "I am not allowed" to navigate my way through the city, country, level of danger and possibly beyond, since eventually I need my employee, with whom I started, to plant a flag on him.

I only know Java, so I don't even know if this is actually possible in other, possibly more functional programming languages.

I could use the following code as a workaround, but it would be better for a cleaner way:

List<Employee> employees = [...];

employees.stream()
    .forEach(employee -> {
        Optional<Integer> lvl = Optional.of(employee)
            .map(Employee::getCity)
            .map(City::getCountry)
            .map(countryService::determineHazardLevel)
            .filter(level -> level > 5); // integer

        lvl.ifPresent(employee.setPriority(HIGH));
    }

      

Perhaps I just got it all wrong, and I need to look at him in a completely different way ...

PS: I just wrote this stuff in notepad, not sure if I made some mistakes, but hopefully an idea comes up. Simply put, I see it this way: Normal flows are commands like A, B, C, D

. But I want to do it A, B, B1, B2, B3, C, D

. But after going down road B, I cannot return to my "main stream".

+3


source to share


2 answers


The simplest solution is to invoke method method calls, just like in Kayamans answers .

employees.stream()
    .filter( employee -> employee.getCity().getCountry().determineHazardLevel() > 5).
    .forEach(employee -> employee.setPriority(HIGH))

      

Alternatively, you can use flow steps in an inner operation:



employees.stream()
    .filter( employee -> Stream.of(employee)
                               .map(Employee::getCity)
                               .map(City::getCountry)
                               .mapToInt(countryService::determineHazardLevel)
                               .anyMatch(level -> level > 5))
    .forEach(employee -> employee.setPriority(HIGH))

      

But for a simple chain of steps, map

Id is left with a chain of normal method calls.

+5


source


No, since the flow is changed from Stream<Employee>

to Stream<Integer>

by operations map()

(after all, what is supposed to do map()

is not there unmap()

).

You can only do this with



employees.stream().
    filter(e -> e.getCity().getCountry().determineHazardLevel() > 5).
    forEach(...)

      

which is ugly but keeps the original Stream<Employee>

. For "less ugly" code, run determineHazardLevel()

in Employee

.

+4


source







All Articles