$show=/label

Java 8 Stream Filter with Lambda Expression + Predicate Filter Examples

SHARE:

A quick guide to Java 8 Stream Filter with Lambda Expression and how to use Stream.filter() method in-stream api along with examples.

1. Overview

In this article, You'll learn and explore the usage of different ways of the Stream.filter() method. 

Let us see the examples programs on how to use it effectively stream filter options and examples with checked exceptions in java. 8 with lambda.

Java 8 Stream Filter with Lambda Expression


2. Using Stream.filter() Method

The Stream API has a filter() method and it is an Intermediate Operations.

filter() syntax:

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

This method runs the given predicate on all the values of stream objects and returns a filtered stream as a returned object.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FilterExample {

    public static void main(String[] args) {

        List<Integer> numbers  =  Arrays.asList(10,50,30,18,35);

        List<Integer> evenNumbers  =  numbers.stream().filter(number ->  number % 2 ==  0).collect(Collectors.toList());

        System.out.println("After applying the filter function : "+evenNumbers);

    }
}
 

Output:

After applying the filter function : [10, 50, 30, 18]

 

3. Filter on Custom Objects

As of now, you have seen how to use filters with a list of string objects. But, now let us see how to use filters on the List of Employee objects.

Creating an Employee class with id, name, and age properties and setters, getters, toString() methods.

public class Employee {

    private int id;
    private String name;
    private int age;

    public Employee(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
	
    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

Create 5 employee objects and add them to a List using Arrays.asList() method.

		Employee e1 = new Employee(100, "Ram baba", 50);
		Employee e2 = new Employee(101, "Tricky joy", 25);
		Employee e3 = new Employee(102, "Johon perth", 45);
		Employee e4 = new Employee(103, "abran parande", 21);
		Employee e5 = new Employee(104, "naha ama", 59);

		List<Employee> emps = Arrays.asList(e1, e2, e3, e4, e5);
 

In the below example, you are going to filter the employees whose ages are greater than 35 using the filter() method.

List<Employee> emps = Arrays.asList(e1, e2, e3, e4, e5);

System.out.println("Emp list size  : " + emps.size());

List<Employee> filteredList = emps.stream().filter(emp -> emp.getAge() < 35).collect(Collectors.toList());

System.out.println("Emp list size after filter() : " + filteredList.size());
 

Output:

Emp list size  : 5
Emp list size after filter() : 2
 

4. Filtering with the collection with multiple criteria

Yes it is allowed to use the filter() method with multiple fields and also filter() method can be called multiple times.

// way 1
List<Employee> multipleCriteriaList = emps.stream()
		.filter(emp -> emp.getAge() > 40 && emp.getName().contains("a")).collect(Collectors.toList());
System.out.println("Multiple fields criteria: way 1 - " + multipleCriteriaList.size());

// way 2
List<Employee> multipleCriteriaList1 = emps.stream().filter(emp -> emp.getAge() > 40)
		.filter(emp -> emp.getName().contains("a")).collect(Collectors.toList());
System.out.println("Multiple fields criteria: way 2 - " + multipleCriteriaList1.size());
 

Both of these methods will produce the same output with size 2.

5. Handling Exceptions with Custom Classes or Checked Exceptions

There are chances that logic inside the filter() method can throw some checked or unchecked exceptions.

for example,  you are calling some method using method reference.

List<Book> validBooks = books.stream().filter(Book::isValidBookLocation).collect(Collectors.toList());

This isValidBookLocation() method throwing an IOException which causes the compile-time error.

Incompatible thrown types java.io.IOException in functional expression

or 

Unhandled exception type IOException

Full Example:

The below program throws the compile-time error

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.net.ssl.HttpsURLConnection;

public class FiterThrowsException {

	public static void main(String[] args) {

		Book b1 = new Book(123, "/bookstore/100/23/book123.pdf");
		Book b2 = new Book(145, "/bookstore/100/45/book145.pdf");
		Book b3 = new Book(167, "/bookstore/100/67/book167.pdf");

		List<Book> books = Arrays.asList(b1, b2, b3);

		List<Book> validBooks = books.stream().filter(Book::isValidBookLocation).collect(Collectors.toList());

	}

}

class Book {
	private long id;
	private String path;

	public Book(long id, String path) {
		this.id = id;
		this.path = path;
	}

	public boolean isValidBookLocation() throws IOException {
		URL url = new URL(this.path);
		HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
		return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
	}

}
 

To handle such exceptions either need to throw the same exception or wrap the code inside a try-catch block.

From lambda expressions, there is no way to throw the exceptions but you can use the try-catch block.

Use the below code to make it work above example.

List<Book> validBooks = books.stream().filter(t -> {
	try {
		return t.isValidBookLocation();
	} catch (IOException e) {
		System.out.println("Errorn while checking the file : "+e.getMessage());
		e.printStackTrace();
	}
	return false;
}).collect(Collectors.toList());
 

If you want to throw any exception from Predicate then wrap it as RuntimeException.

6. Exception Handling Using ThrowingFunction

Furthermore, There is another way to handle the exceptions from stream or lambda using an opensource library.

Add the following the dependency in the pom.xml file or add the jar to the project.

<dependency>
	<groupId>pl.touk</groupId>
	<artifactId>throwing-function</artifactId>
	<version>1.3</version>
</dependency>
 

To handle such unchecked exceptions, touk api provided with a class ThrowingFunction and which. has unchecked() method. to wrap the throwing exception inside the predicate function.

List validBooks = books.stream().filter(ThrowingFunction.unchecked(Book::isValidBookLocation))
 								.collect(Collectors.toList());
 

But this code does not work because the filter() method needs the Predicate but unchecked() returns Function type. So, this will not work here. I have seen in the other websites showing the examples like passing the output of the unchecked() method to the filter()  method. 

But, you can pass this to the Stream.map() method as below.

Stream.map() vs Stream.filter()

Stream<Boolean> validBooks = books.stream().map(ThrowingFunction.unchecked(Book::isValidBookLocation));
 

7. Conclusion

In this article, you've seen how to use the filter() method in context of lambda expression in java 8  and. how to handle the unchecked exceptions from predicate function.

Strem filter ref

ThrowingFunction

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 Stream Filter with Lambda Expression + Predicate Filter Examples
Java 8 Stream Filter with Lambda Expression + Predicate Filter Examples
A quick guide to Java 8 Stream Filter with Lambda Expression and how to use Stream.filter() method in-stream api along with examples.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh49U-LOl1ioLG6-OaiXvw6uCL6nHxsx0AW8fiJhV18QXOsW78cz5NQpMNNs23F8VnQkQhPFwrC_5cQdDPfIXvC5hRrbFmTRScu8CH6AXb6YDTDdogG3WGWQ06kZlGNaHuXWyYSqT4_yIQ/w640-h390/Java+8+Stream+Filter+with+Lambda+Expression.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh49U-LOl1ioLG6-OaiXvw6uCL6nHxsx0AW8fiJhV18QXOsW78cz5NQpMNNs23F8VnQkQhPFwrC_5cQdDPfIXvC5hRrbFmTRScu8CH6AXb6YDTDdogG3WGWQ06kZlGNaHuXWyYSqT4_yIQ/s72-w640-c-h390/Java+8+Stream+Filter+with+Lambda+Expression.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/09/java-8-stream-filter-with-lambda.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/09/java-8-stream-filter-with-lambda.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content