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.



Our Best Partners: lucky jet predictor for pc minas jogo lucky jet fake game nettikasino ilman tiliä 幻界エロス教典 竹取物語 moon princess 映画 レビュー slot pg demo jogos de aposta minas viro nettikasinot pikakasinot ilman rekisteröitymistä pay and play kasinot

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply