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 both are return with String.
Here is my sample code:
List<Client> clients = new ArrayList<>();
List<User> users = new ArrayList<>();
List<Client> results = new ArrayList<>();
for (Client user : users) {
for(Client client: clients){
if(user.getName().equals(client.getUserName())){
result.add(client);
}
}
}
I would like share an example to understand the usage of stream().filter
Code Snippet: Sample program to identify even number.
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.
Something like:
This is however not an awfully efficient way to do it. Unless the collections are very small, you will be better of building a set of user names and using that in the condition.
But this is quite inefficient, because it's O(m * n). You'd better create a Set of acceptable names:
Also note that it's not strictly equivalent to the code you have (if it compiled), which adds the same client twice to the list if several users have the same name as the client.
Look this: