Java 8 filters list
A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always sluggish, invoking filter() creates a brand-new stream that includes all of the items from the existing stream that, when viewed, match the given predicate.
Syntax
Stream<T> filter(Predicate<? super T> condition)
Stream filter() in Java:
In this illustration, we first build a stream using the stream() function from such a list of places names, and afterward, we generate a second stream of lengthy places names using the stream methodology. As stated before, the stream filter transforms data through one stream into information from a different stream.
Execution steps:
Eliminating items that may be divided into numbers from 0 to 10.
Removing entries that begin with an uppercase letter at a certain index.
Removing components that terminate in a specific alphabetical order of the letter.
FilterExample1.java
//This program is for the implementation of the java 8 filters
//by using the Stream filter()
//importing the required packages
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
// class FilterExample1
public class FilterExample1
{
// main code of the program
public static void main(String args[])
{
//A list is created
List<String> placenames=
Arrays.asList("Telangana","Andhra","Chennai","Kerala","India");
//the stream was created, which consists of the names
Stream<String> placename = placenames.stream();
//Another stream longPlacenames was created for the identification of long names
Stream<String> longPlacenames = placename.filter(str -> str.length() > 6);
//the result of the long place names is displayed
System.out.println("Names of the long places");
longPlacenames.forEach(str->System.out.print(str+" "));
}
}
Output:

Multiple criteria for the Java Stream filter()
We observe that the filter() method will only have one criterion inside the code below. We may combine many conditions inside the filter() function using Java's logical operators. In the example below, two filter method criteria are connected by the (&&) logical operator.
FilterExample2.java
//This program is for the implementation of the java 8 filters
//by using the Stream filter()
//importing the required packages
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// class FilterExample2
public class FilterExample2
{
// main code of the program
public static void main(String args[])
{
//A list is created
List<String> placenames=
Arrays.asList("Telangana","Andhra","Chennai","Kerala","India");
//giving the multiple conditions for printing the result
List<String> longPlacenames = placenames.stream().filter(str -> str.length() > 3 && str.length() <= 6).collect(Collectors.toList());
//Printing the result place which has the given conditions
System.out.println("The required places are: ");
longPlacenames.forEach(System.out::println);
}
}
Output

Stream filter() ,map() in Java Filter
The map() function and stream filter() is applied in the following Java code to generate the square of the given integers.
FilterExample3.java
//This program is for the implementation of the java 8 filters
//by using the Stream filter()
//importing the required packages
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// class FilterExample2
public class FilterExample2
{
// main code of the program
public static void main(String args[])
{
//A list is created
List<Integer> givennumbers = Arrays.asList(1,3,5,7,9);
//an object squareoftheNumber is created for squaring the given number
List<Integer> squareoftheNumber = givennumbers.stream()
.map(n -> n * n).
collect(Collectors.toList());
//Printing the result as the square of the given numbers
System.out.println("The Square of the given numbers: ");
System.out.println(squareoftheNumber);
}
}
Output:

Stream filter() along with the collect() in Java Filter
The program below will describe the Java filter, which also consists of the collect() method.
FilterExample3.java
//This program is for the implementation of the java 8 filters
//by using the Stream filter() and the collector() method
//importing the required packages
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// class FilterExample3
public class FilterExample3
{
// main code of the program
public static void main(String args[])
{
//A list is created
List<String> places=
Arrays.asList("Telangana","Andhra","Chennai","Kerala","India");
List<String> display = places.stream()
.filter(line -> !"Chennai".equals(line))
.collect(Collectors.toList());
// the result is printed according to the given condition
System.out.println("The places are: ");
display.forEach(System.out::println);
}
}
Output:

Java Stream filter() with the functions min() and max()
This program will print results of the minimum, and the maximum numbers in the list declared.
FilterExample4.java
//This program is for the implementation of the java 8 filters
//by using the Stream filter() and min() and max() methods
//importing the required packages
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Collection;
// class FilterExample3
public class FilterExample4
{
// main code of the program
public static void main(String args[])
{
//A list is created for storing the numbers
Collection<Integer> numbers = Arrays.asList(10,20,30,40,50);
Integer min = numbers.stream().min(Integer::compareTo).get();
Integer max = numbers.stream().max(Integer::compareTo).get();
//printing the result of the maximum and minimum numbers among the list
System.out.println("The lowest value is: "+min);
System.out.println("The Largest value is: "+max);
}
}
Output:
