Difference between map and filter on a java collection stream
Solution 1
By using map, you transform the object values.
The map operation allows us to apply a function, that takes in a parameter of one type, and returns something else.
Filter is used for filtering the data, it always returns the boolean value. If it returns true, the item is added to list else it is filtered out (ignored) for eg :
List<Person> persons = …
Stream<Person> personsOver18 = persons.stream().filter(p -> p.getAge() > 18);
For more details on this topic you can visit this link
Solution 2
map returns a stream consisting of the results of applying the given function to the elements of this stream. In a simple sentence, the map returns the transformed object value.
For example, transform the given Strings from Lower case to Upper case by using the map().
myList.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
filter returns a stream consisting of the elements of this stream that match the given predicate. In a simple sentence, the filter returns the stream of elements that satisfies the predicate.
For example, find the strings which are starting with 'o' by using the filter.
myList.stream().filter(s -> s.startsWith("o")).forEach(System.out::println);
Program:
import java.util.ArrayList;
import java.util.List;
public class MapAndFilterExample {
static List<String> myList = null;
static {
myList = new ArrayList<String>();
myList.add("okay");
myList.add("omg");
myList.add("kk");
}
public static void main(String[] args) {
System.out.println("Converting the given Strings from Lower case to Upper case by using map");
myList.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
System.out.println("filter which is starting with 'o' by using filter");
myList.stream().filter(s -> s.startsWith("o")).forEach(System.out::println);
}
}
Solution 3
Filter takes a predicate as an argument so basically you are validating your input/collection against a condition, whereas a map allows you to define or use a existing function on the stream eg you can apply String.toUpperCase(...)
etc. and transform your inputlist
accordingly.
Related videos on Youtube
Keerthikanth Chowdary
Hi, I am keerthikanth. I like exploring new technologies and like open source projects.
Updated on January 28, 2021Comments
-
Keerthikanth Chowdary over 3 years
Suppose there is a list say
List<String> myList = new ArrayList<String>(); myList.add("okay"); myList.add("omg"); myList.add("kk");
I am doing this:
List<String> fianllist = myStream.map(item -> item.toUpperCase()).filter(item ->item.startsWith("O")).collect(Collectors.toList());
My question is what the difference between map and filter as both can take a lambda expression as a parameter. Can some one please explain?
-
Eran about 8 yearsDid you read the Javadoc? They do completely different things.
-
Joop Eggen about 8 years
map
maps the passed stream element (String) to something else, here an other String value, but it could also have been an Integer.filter
removes elements not fitting the condition. -
sergiu about 8 yearsthink of it as a database query. select name(the map) from users where name="John" (the filter).
-
Keerthikanth Chowdary about 8 yearsThanks for the clarification
-