Java Stream
class provides a filter
method to filter out elements based on the predicate
provided. The filter
method is an intermediate
operation, it takes the stream
as input, filters out the element and returns the new stream
to another intermediate or terminal operation.
Filter
does not modify the existing stream but creates a new stream of allowed elements and passes to the next operation.
The filter
method takes Predicate
as input, which means lambda expression can be directly passed here as well.
package org.wesome.java8;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
class Apple {
public static void main(String args[]) {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println("*---------------------------Traditional method---------------------------*");
for (Integer integer : integers) {
if ((integer % 2 == 0)) {
System.out.println(integer);
}
}
Predicate<Integer> even = i -> i % 2 == 0;
System.out.println("*---------------------------Predicate---------------------------*");
integers.stream().filter(even).forEach(System.out::println);
System.out.println("*---------------------------Predicate as method argument---------------------------*");
integers.stream().filter(integer -> filteredList(integer, even)).forEach(System.out::println);
integers.stream().filter(even).forEach(System.out::println);
System.out.println("*---------------------------Inline Predicate---------------------------*");
integers.stream().filter(integer -> integer % 2 == 0).forEach(System.out::println);
}
private static boolean filteredList(Integer integer, Predicate predicate) {
return predicate.test(integer);
}
}
Filter on Map
The map takes Key and value, so a simple filter cannot be applied on Map directly.
package org.wesome.java8;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
class Apple {
public static void main(String args[]) {
Map<Integer, String> map = new HashMap();
map.put(1, "Macintosh");
map.put(2, "Fuji");
map.put(3, "Gala");
map.put(4, "Jonagold");
System.out.println("*---------------------------Traditional method---------------------------*");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
if ((entry.getKey() % 2 == 0)) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
Predicate<Map.Entry<Integer, String>> even = i -> i.getKey() % 2 == 0;
System.out.println("*---------------------------Predicate---------------------------*");
map.entrySet().stream().filter(even).forEach(System.out::println);
System.out.println("*---------------------------Predicate as method argument---------------------------*");
map.entrySet().stream().filter(entry -> filteredList(entry, even)).forEach(System.out::println);
System.out.println("*---------------------------Inline Predicate---------------------------*");
map.entrySet().stream().filter(entry -> entry.getKey() % 2 == 0).forEach(System.out::println);
}
private static boolean filteredList(Map.Entry<Integer, String> map, Predicate predicate) {
return predicate.test(map);
}
}
Filter on User Defined Object
package org.wesome.java8;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
@Data
@AllArgsConstructor
class Fruit {
int fruitId;
String fruitName;
}
class Apple {
public static void main(String args[]) {
List<Fruit> fruits = Arrays.asList(new Fruit(1, "Macintosh"), new Fruit(2, "Fuji"), new Fruit(3, "Gala"), new Fruit(4, "Jonagold"));
System.out.println("*---------------------------Traditional method---------------------------*");
for (Fruit fruit : fruits) {
if (fruit.getFruitId() % 2 == 0) {
System.out.println(fruit);
}
}
Predicate<Fruit> even = i -> i.getFruitId() % 2 == 0;
System.out.println("*---------------------------Predicate---------------------------*");
fruits.stream().filter(even).forEach(System.out::println);
//
System.out.println("*---------------------------Predicate as method argument---------------------------*");
fruits.stream().filter(fruit -> filteredList(fruit, even)).forEach(System.out::println);
//
System.out.println("*---------------------------Inline Predicate---------------------------*");
fruits.stream().filter(fruit -> fruit.getFruitId() % 2 == 0).forEach(System.out::println);
}
private static boolean filteredList(Fruit fruit, Predicate predicate) {
return predicate.test(fruit);
}
}