I have two list and i want filter thoose elements which are both list contains. And i want to do this with lambda expression.
Users getName and Clients getUserName b
I would like share an example to understand the usage of stream().filter
Code Snippet: Sample program to identify even number.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public void fetchEvenNumber(){
List numberList = new ArrayList<>();
numberList.add(10);
numberList.add(11);
numberList.add(12);
numberList.add(13);
numberList.add(14);
numberList.add(15);
List evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());
System.out.println(evenNumberListObj);
}
Output will be : [10, 12, 14]
List evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());
numberList: it is an ArrayList object contains list of numbers.
java.util.Collection.stream() : stream() will get the stream of collection, which will return the Stream of Integer.
filter: Returns a stream that match the given predicate. i.e based on given condition (i -> i%2 != 0) returns the matching stream.
collect: whatever the stream of Integer filter based in the filter condition, those integer will be put in a list.