Stream filter() method in Java8 with examples

In Java, the filter() method is part of the Stream API and is used to filter elements from a stream based on a specified condition. The filter() method returns a new stream that contains only the elements that satisfy the specified condition

Table of Content :

Here's the syntax for using the filter() method in Java:

Stream<Tgt filter(Predicate<? super T> predicate)

where T is the type of the elements in the stream, and predicate is a Predicate functional interface that is used to test each element in the stream. The Predicate functional interface takes an element of type T as input and returns a boolean value indicating whether the element satisfies the specified condition.

Here's an example usage of the filter() method in Java:

Using the Boolean.toString() method:

List<Integergt numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Stream<Integergt evenNumbers = numbers.stream().filter(num -> num % 2 == 0); evenNumbers.forEach(System.out::println); // prints 2 4 6 8 10

Stream filter() and collect() method examples:

Let's say we have a list of names and we want to filter out only the names that start with the letter "J" and store them in a new list. We can use the filter() method to filter out the elements that match our condition, and then use the collect() method to collect the filtered elements into a new list.

import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class FilterAndCollectExample { public static void main(String[] args) { List<String> names = Arrays.asList("Jones", "Jara", "John", "Jane", "Mary", "Jack"); List<Stringgt names1 = names.stream().filter(name -> name.startsWith("J")). collect(Collectors.toList()); System.out.println(names1); // [Jones, Jara, John, Jane, Jack] } }

Stream filter() and map() method to find even numbers and Square them

import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamFilterAndMapMethod { public static void main(String[] args) { System.out.println("Filter out the even numbers and find Square for them : \n"); List<Integergt numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integergt newList = numbers .stream() .filter(i -> i%2 != 0) .map(n -> n * n ) .collect(Collectors.toList()); newList.forEach(s -> System.out.println(s)); } } output: Filter out the even numbers and find Square for them : 1 9 25 49 81

Find duplicates in given String list Using filter

import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class FindDuplicatesUsingFilterAndSetAddMethod { public static void main(String[] args) { List<Stringgt companies = new ArrayList(); companies.add("Facebook"); companies.add("Flipkart"); companies.add("Walmart"); companies.add("Jio"); companies.add("Airtel"); companies.add("Idea"); companies.add("HDFC"); companies.add("Jio"); companies.add("Flipkart"); Set<Stringgt distinctCompanies = new HashSet(); Set<Stringgt duplicates = companies.stream().filter(company-> !distinctCompanies .add(company)) .collect(Collectors.toSet()); System.out.println(duplicates); } }

In this Article we have seen what is filter in java Stream API with examples