繁体   English   中英

检查列表是否包含 java 中另一个列表的任何项目

[英]Check if list contains any item of another list in java

我们有两个列表,一个是Question类型,另一个是Tag类型。

问题 class 具有这些属性

private String id;
private String header;
private String content;
private List<Tag> tags;
private Long timeStamp;

问题列表中有几个问题,标签列表中有所有标签。 我们想检查一个问题是否包含标签列表的任何标签。 我想对所有问题都这样做。

使用question.getTags ,我得到标签列表。

我试过了

List<Question> allQuestions =  ... ; // List of type questions
List<Tags> alltags = ... ;  // List of type tag

for(Question question: allQuestions) {
    for(Tag tag: allTags){
        if(question.getTags().contains(tag)) {
            //do something
        }
    }
}

这并不是我想做的事情,我想我必须对流做一些事情,但我不太清楚我究竟是如何编写代码的。

如果列表中有任何标签,您正在为列表中的每个标签执行操作,而不是每个问题一次。

正如您所建议的,使用流可以使这个解决方案更容易:

allQuestions.forEach(question -> {
    if (question.getTags().stream().anyMatch(tag -> allTags.contains(tag)) {
        // do something
    }
});

注意 - 这仍然具有 O(m * n) 运行时复杂度,其中 m 是问题的数量,n 是标签的数量。 您可以通过从标签列表创建一个Set其优化为 O(m + n) 运行时复杂度,以便contains操作具有 O(1) 时间复杂度。

尝试:

for(Question question: allQuestions) {
    for(Tag tag: allTags){
        if(question.contains(tag)) {
            //do something
        }
    }
}

或者您是在询问给定问题是否包含 question.tags() 列表中的标记?

List<Question> allQuestions =  ... ; // List of type questions
List<Tags> alltags = ... ;  // List of type tag

for (Question x : allQuestions) {
    List<Tag> questionTags = new ArrayList<>();
    questionTags = x.getTags();
    questionTags.retainAll(allTags);
    // questionTags will retain common tags between allTags and x.getTags()            

    for (Tag tag: questionTags) {
        // Execute when there is at least one common tag
    }
}

获取所有标签列表中包含的所有标签及其出现alltags

Map<Tag, Long> map = allQuestions.stream()
                           .flatMap(q -> q.getTags().stream())
                           .filter(t -> allTags.contains(t))
                           .collect(Collectors.groupingBy(e -> e, Collectors.counting()));

然后,您可以根据需要将此列表添加到另一个列表中。 并建议您为alltags设置一个集合,然后它会更快,因为allTagsSet.contains需要O(1)

Apache commons 有一个实用方法: CollectionUtils#containsAny

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM