×

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:

Java 8 filters list

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

Java 8 filters list

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:

Java 8 filters list

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 8 filters list

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:

Java 8 filters list

Related Topics

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Array and String based questions in Java

1. What is an Array in Java? A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of...

4 minutes read.

Java Architecture

Java architecture is a combination of three parts they are JVM, JRE and JDK. These components will help in the functioning of the java programs. The process of code interpretation...

6 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long. Syntax: public static int incrementExact (int a)public static...

1 minute read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.