MainMenu

Home Java Overview Maven Tutorials

Monday 6 November 2017

Java 8 Stream Filter with Example


Hello friends,

In this article we will discuss about Stream Filter in Java:


For Video :- Click Here



we will provide java 8 Stream filter() example.
We filter a collection for a given Predicate instance. filter() method returns a Stream instance which consists only filtered element on the basis of given Predicate.

As the name says , Stream, means which is flowing continuously like data in list, and we want to filter some result from this list as per your need.
By below example this will get more clear.
Code :


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Swap
{
public static void main(String args[])
{
List list = new ArrayList();
list.add("chandan");
list.add("adhiraj");
list.add("Akshat");
list.add("Anant");
List names = Arrays.asList("aviraj", "angraj", "devraj", "dananjay" );
list.stream().filter( u -> u.startsWith("ch")).forEach(System.out::println );
names.stream().filter( u -> u.endsWith("aj")).forEach(System.out::println );
names.stream().map(u -> u.equals("aviraj")).forEach(System.out::println);
}
}


OutPut:

chandan
aviraj
angraj
devraj
true
false
false
false


Code Explanation : First we create two list with name list & names then we will add some records in the both list.
then simply we will apply , Stream() method on the lists & filter() them as per our need.
finally we will print the required string.


for more deep knowledge :
Click here



Tags :
Java 8Stream
FilterStream filter with example

No comments:

Post a Comment