Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Lambda filter by Lists

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);         }     } } 
like image 784
eszik.k Avatar asked May 03 '15 09:05

eszik.k


People also ask

How do I filter a list based on another list in Java 8?

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.

What is the return type of filter in Java 8?

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.

How to filter a list with a predicate in Java 8?

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.

How to link two conditions in the filter method in Java?

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.

How to filter collections in Java 8?

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:

How to handle exceptions in lambda expressions in Java?

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:


2 Answers

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.

like image 178
JB Nizet Avatar answered Sep 29 '22 10:09

JB Nizet


Look this:

List<Client> result = clients     .stream()     .filter(c ->          (users.stream().map(User::getName).collect(Collectors.toList())).contains(c.getName()))         .collect(Collectors.toList()); 
like image 42
LeoMandrak Avatar answered Sep 29 '22 10:09

LeoMandrak