×

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

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

How Many Ways to Create an Object in Java?

As you know, a class is a blueprint for an object, and you can create objects from it. There are several ways to create objects of classes in Java. This...

6 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.