Java 8 Stream API filter() example





Hey guys in this post let’s discuss about filter() introduced in Java 8 java.util.stream.Steam interface. On the streams() we can perform some aggregate operations such as filter(), sorted() and map(). In this post let’s discuss about filter().

Example1.java

public class Test {
	public static void main(String[] args) throws IOException {
		List<String> fruits = Arrays.asList("Apple", "Banana", "Apple", "Mango", "Orange", "Apple");
		List<String> filteredList = new ArrayList<String>();
		//old way
                for (String string : fruits) {
			if (string.equals("Apple")) {
				filteredList.add(string);
			}
		}
		//traditional way
		filteredList = fruits.stream().filter(fruit -> fruit.equals("Apple")).collect(Collectors.toList());
		filteredList.forEach(System.out::println);
		
	}
}

Here collect(), method of the Stream interface performs a mutable reduction operation on the elements of steam using a Collector.

fruits, is the source and on the source we can call stream() method, then we can chain the aggregate methods to perform the operations.

Example2.java

public class Test {
	public static void main(String[] args) throws IOException {
		List<String> fruits = Arrays.asList("Apple", "Banana", "Apple", "Mango", "Orange", "Apple");
		List<String> filteredList = new ArrayList<String>();
		//old way
                for (String string : fruits) {
			if (!string.equals("Apple")) {
				filteredList.add(string);
			}
		}
		//traditional way
		filteredList = fruits.stream().filter(fruit -> !fruit.equals("Apple")).collect(Collectors.toList());
		filteredList.forEach(System.out::println);
		
	}
}

Example3.java

public class Test {
	public static void main(String[] args) throws IOException {
		List<String> fruits = Arrays.asList("Apple", "Banana", "Apple", "Mango", "Orange", "Apple");
		long count = 0;
		//old way
                for (String string : fruits) {
			if (string.equals("Apple")) {
				count++;
			}
		}
                System.out.println(count);
		count = 0;
		//traditional way
		count = fruits.stream().filter(fruit -> fruit.equals("Apple")).count();
		System.out.println(count);
		
	}
}

Let’s look at some examples on Objects. Let’s consider the Person object.

Example4.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.swing.plaf.synth.SynthSeparatorUI;

public class Test {
	public static void main(String[] args) throws IOException {
		
		List<Person> list = Arrays.asList(
				new Person("Bushan", 28, "India"),
				new Person("Ramesh", 38, "Australia"),
				new Person("Sukesh", 48, "South Africa"),
				new Person("Bharath", 32, "India"),
				new Person("Chethan", 36, "India")
		);
		//traditional way
		List<Person> filteredList = new ArrayList<>();
		for (Person person : list) {
			if (person.getCountry().equals("India")) {
				filteredList.add(person);
			}
		}
		System.out.println(filteredList);
		
		//using Stream filter()
		filteredList = list.stream().filter(p -> p.getCountry().equals("India")).collect(Collectors.toList());
		filteredList.forEach(System.out::println);
	}
}

class Person{
	private int id;
	private String name;
	
	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 Product(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + "]";
	}
}

That’s it for this post. Let’s discuss about sorted() in the next post.



About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *