Java 8 Lambda 过滤器列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30012295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:59:01  来源:igfitidea点击:

Java 8 Lambda filter by Lists

javalambdajava-8

提问by eszik.k

I have two list and i want filter thoose elements which are both list contains. And i want to do this with lambda expression.

我有两个列表,我想过滤两个列表都包含的元素。我想用 lambda 表达式来做到这一点。

Users getName and Clients getUserName both are return with String.

用户 getName 和 Clients getUserName 都返回字符串。

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);
        }
    }
}

采纳答案by JB Nizet

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:

但这效率很低,因为它是 O(m * n)。您最好创建一组可接受的名称:

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.

另请注意,它并不严格等同于您拥有的代码(如果已编译),如果多个用户与客户端具有相同的名称,则该代码会将同一客户端两次添加到列表中。

回答by daphshez

Something like:

就像是:

clients.stream.filter(c->{
   users.stream.filter(u->u.getName().equals(c.getName()).count()>0
}).collect(Collectors.toList());

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.

然而,这并不是一种非常有效的方法。除非集合非常小,否则最好构建一组用户名并在条件中使用它。

回答by LeoMandrak

Look this:

看看这个:

List<Client> result = clients
    .stream()
    .filter(c -> 
        (users.stream().map(User::getName).collect(Collectors.toList())).contains(c.getName()))
        .collect(Collectors.toList());

回答by Eswaran Venkatesan

I would like share an example to understand the usage of stream().filter

我想分享一个例子来理解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<Integer> numberList = new ArrayList<>();
        numberList.add(10);
        numberList.add(11);
        numberList.add(12);
        numberList.add(13);
        numberList.add(14);
        numberList.add(15);

        List<Integer> evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());
        System.out.println(evenNumberListObj);
}

Output will be : [10, 12, 14]

输出将是:[10, 12, 14]

List evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());

List evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());

numberList: it is an ArrayList object contains list of numbers.

numberList:它是一个包含数字列表的 ArrayList 对象。

java.util.Collection.stream(): stream() will get the stream of collection, which will return the Stream of Integer.

java.util.Collection.stream(): stream() 会得到集合的流,它会返回整数的流。

filter: Returns a stream that match the given predicate. i.e based on given condition (i -> i%2 != 0) returns the matching stream.

filter:返回匹配给定谓词的流。即基于给定的条件 (i -> i%2 != 0) 返回匹配的流。

collect: whatever the stream of Integer filter based in the filter condition, those integer will be put in a list.

collect:无论基于过滤条件的整数过滤器流如何,这些整数都将放入列表中。