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