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); } } }
One of the utility method filter() helps to filter the stream elements that satisfy the provided criteria. The predicate is a functional interface that takes a single element as an argument and evaluates it against a specified condition.
The filter() method returns a Stream of Dishes and the collect() method converts the Stream into a List. The 'collect' operation is called a terminal operation.
First the list is converted into a stream. This stream is then filtered with the predicate and the stream is converted back into a list with the collect () method. Thanks to the lambda notation of Java 8, the Predicate can also be passed as a simple function, which shortens the expression even more: makes the filtering even clearer.
Two conditions in the filter method are linked using the (&&) logical operator in the following example. In the following Java code, the stream filter () is used along with map () method to print squares of given numbers. In the following Java code, the stream filter () is used along with collect () method.
A quick tutorial to filtering collections in Java using different approaches. Quick and practical guide to Functional Interfaces present in Java 8. 2. Using Stream.filter () The filter () method is an intermediate operation of the Stream interface that allows us to filter elements of a stream that match a given Predicate:
Indeed, the functional interfaces in Java don't declare any checked or unchecked exceptions. Next we're going to show some different ways to handle exceptions in lambda expressions. 3.1. Using a Custom Wrapper First, we'll start by adding a profilePhotoUrl to our Customer:
Predicate<Client> hasSameNameAsOneUser = c -> users.stream().anyMatch(u -> u.getName().equals(c.getName())); return clients.stream() .filter(hasSameNameAsOneUser) .collect(Collectors.toList());
But this is quite inefficient, because it's O(m * n). You'd better create a Set of acceptable names:
Set<String> acceptableNames = users.stream() .map(User::getName) .collect(Collectors.toSet()); return clients.stream() .filter(c -> acceptableNames.contains(c.getName())) .collect(Collectors.toList());
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:
List<Client> result = clients .stream() .filter(c -> (users.stream().map(User::getName).collect(Collectors.toList())).contains(c.getName())) .collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With