Does the perfomance of "filter then map" and "map then filter" differ in a Stream?

Keyzj :

I would like to know what is faster: to filter a custom object by field and then map by its field or vice-versa (map and then filter).
At the end, I usually want to collect the mapped field into some Collection.

For example, the simplest Person class:

public class Person {
    String uuid;
    String name;
    String secondName;
}

Now let's have a List<Person> persons.

List<String> filtered1 = persons
                .stream()
                .filter(p -> "NEED_TOY".equals(p.getName()))
                .map(Person::getName)
                .collect(Collectors.toList());
// or?
List<String> filtered2 = persons
                .stream()
                .map(Person::getName)
                .filter(p -> "NEED_TOY".equals(p))
                .collect(Collectors.toList());
JB Nizet :

In this specific example, where calling Person.getName() has basically no cost at all, it doesn't matter, and you should use what you find the most readable (and filtering after could even be marginally faster, since as TJ mentions, the mapping operation is part of the filtering operation).

If the mapping operation has a significant cost however, then filtering first (if possible) is more efficient, since the stream won't have to map the elements that have been filtered out.

Let's take a contrived example: you have a stream of IDs, and for every even ID in the stream, you have to execute an http GET request or a database query to get the details of the item identified by this ID (and thus mapping the ID to a detailed object).

Assuming that the stream is composed of half even and half odd IDs, and each request takes the same time, you would divide the time by two by filtering first. If every http request takes 1 second and you have 60 IDs, you would go from 60 seconds to 30 seconds for the same task by filtering first, and you would also reduce the charge on the network and the external http API.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=138831&siteId=1