How to use Java 8 Streams Filter in Java Collections:-

In this article, we are going to discuss how to use Java 8 Streams Filter effectively in java collections.We are going to demonstrate examples of Java 8 streams filter using fliter() , collect() , distinct() , limit() , findany() .Lets try and understand with following use case.

//pagead2.googlesyndication.com/pagead/js/adsbygoogle

we are going to create a list of Users and perform some operations such as finding Users on basis of salary.Let's See how we will do it prior to Java 8.

Create a user Class:-

int id; String name; int age; public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "User \[id=" + id + ", name=" + name + ", 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; } }

Using stream filter() and map():-

We are going to implement a simple use case to find users whose age is greater than 20 from the above list and try and understand the core usage of Java 8 streams filter.

In Java 7:-

import java.util.List; public class TestLambda{ public static void main(String\[\] args) { List<User> Alluser=new ArrayList<User>(); Alluser.add(new User(1, "Frugalis",23)); Alluser.add(new User(2, "Sanjay",12)); Alluser.add(new User(3, "John",10)); Alluser.add(new User(4, "Sasha",25)); Alluser.add(new User(5, "Virat",20)); Alluser.add(new User(6, "Sachin",16)); Alluser.add(new User(7, "Shikar",12)); Alluser.add(new User(8, "John",25)); Alluser.add(new User(9, "sasha",25)); List<String> allname=new ArrayList<String>(); for(int i=0;i<Alluser.size();i++){ if(Alluser.get(i).getAge()>20){ allname.add(Alluser.get(i).getName()); } } System.out.println(allname); } }

In Java8:-

import java.util.List; import java.util.stream.Collector; import java.util.stream.Collectors; public class LambdaTest{ public static void main(String\[\] args) { List<User> Alluser=new ArrayList<User>(); Alluser.add(new User(1, "Frugalis",23)); Alluser.add(new User(2, "Sanjay",12)); Alluser.add(new User(3, "John",10)); Alluser.add(new User(4, "Sasha",25)); Alluser.add(new User(5, "Virat",20)); Alluser.add(new User(6, "Sachin",16)); Alluser.add(new User(7, "Shikar",12)); Alluser.add(new User(8, "John",25)); Alluser.add(new User(9, "sasha",25)); List<String> allname=new ArrayList<String>(); allname=Alluser.stream(). filter(d->d.getAge()>20). map(User::getName()). collect(Collectors.toList()); System.out.println(allname); } }

Output:-

So looking at both the code you might wonder what is the big deal in Java 8 Streams, just a syntactical difference? Now think about Sorting and getting top 3 elements or getting first three. The Java 7 code would ask you to add more logical operators or conditional blocks.

//pagead2.googlesyndication.com/pagead/js/adsbygoogle

We are coding using Java 8 streams filter here.The main advantage of using this syntax is we are coding in a declarative way.We are declaring what we want to achieve instead of implementing a complex control flow statement.There are a lot of other advantages as well which we are not going to cover here.

One Point to highlight is we are using, _**map(User::getName)**_ , so map takes a functional interface as the parameter in Java8 Streams.we can also use a functional method as well:-

List<String> allname=Alluser.stream(). filter(d->d.getAge()>20). map(d->d.getName()). collect(Collectors.toList());

output:-

Using Java 8 streams filter() , distinct() and limit():-

The Use case we target here is to find first three different users whose age is greater than 20.

Java 8:-

import java.util.List; import java.util.stream.Collectors; public class LambdaTest { public static void main(String\[\] args) { List<User> Alluser=new ArrayList<User>(); Alluser.add(new User(1, "Frugalis",23)); Alluser.add(new User(2, "Sanjay",12)); Alluser.add(new User(3, "John",10)); Alluser.add(new User(4, "Sasha",25)); Alluser.add(new User(5, "Virat",20)); Alluser.add(new User(6, "Sachin",16)); Alluser.add(new User(7, "Shikar",12)); Alluser.add(new User(8, "John",25)); Alluser.add(new User(9, "sasha",25)); List<String> allname=new ArrayList<String>(); allname=Alluser.stream(). filter(d->d.getAge()>20). map(d->d.getName()). distinct(). limit(3). collect(Collectors.toList()); System.out.println(allname); //We can Also Write Like this below , that will look a bit familiar allname.clear() allname = Alluser.stream(). filter(d -> { return d.getAge() > 20; }). map(d ->{ return d.getName(); }) .distinct(). limit(3) .collect(Collectors.toList()); System.out.println(allname); } }

Output:-

We are using Java 8 Streams filter with. distinct() and limit(3)

Using Java 8 streams filter() , findAny() and orElse():-

The Use case we target here is to find any single user whose age is greater than 20 and name is John.

Just trying to demonstrate how to add multiple conditions inside Java 8 streams filter.

Java 8:-

import java.util.List; import java.util.stream.Collectors; public class lambda4 { public static void main(String\[\] args) { List<User> Alluser = new ArrayList<User>(); Alluser.add(new User(1, "Frugalis", 23)); Alluser.add(new User(2, "Sanjay", 12)); Alluser.add(new User(3, "John", 11)); Alluser.add(new User(4, "Sasha", 25)); Alluser.add(new User(5, "Virat", 20)); Alluser.add(new User(6, "Sachin", 16)); Alluser.add(new User(7, "Shikar", 12)); Alluser.add(new User(8, "John", 25)); Alluser.add(new User(9, "sasha", 25)); List<String> allname = new ArrayList<String>(); User user = Alluser.stream() .filter(p -> "John".equals(p.getName()) && p.getAge()>20) .findAny() .orElse(null); System.out.println("Output is :" + user); } }

Output:-

Note:- Visit this Site to Understand More about Basics Of Java and Collections.

Some Must Read Posts