检查一个列表是否包含另一个列表中的元素 [英] Check if one list contains element from the other

查看:165
本文介绍了检查一个列表是否包含另一个列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  List< Object1>列表1; 
列表< Object2>列表2;

我想根据特定属性检查list1中是否存在元素(Object1和Object2) (其中包括一个),一个共同的属性(类型为Long),名为attributeSame)。

现在,我这样做:

  boolean found = false; (Object1 object1:list1){
for(Object2 object2:list2){
if(object1.getAttributeSame()== object2.getAttributeSame()){
found =真正;
//也做

$ b $ if(!found){
//做某事
}
found = false;

$ / code>

但是我认为有一个更好更快的方法来做到这一点:)
有人可以提出吗?

谢谢!

解决方案

这可以通过基本的JDK来完成,而无需修改一行中的输入列表。

 !Collections.disjoint(list1,list2) ; 


I have two lists with different objects in them.

List<Object1> list1;
List<Object2> list2;

I want to check if element from list1 exists in list2, based on specific attribute (Object1 and Object2 have (among others), one mutual attribute (with type Long), named attributeSame).

right now, I do it like this:

boolean found = false;
for(Object1 object1 : list1){
   for(Object2 object2: list2){
       if(object1.getAttributeSame() == object2.getAttributeSame()){
           found = true;
           //also do something
       }
    }
    if(!found){
        //do something
    }
    found = false;
}

But I think there is a better and faster way to do this :) Can someone propose it?

Thanks!

解决方案

This can be done with the basic JDK without modifying the input lists in the one line

!Collections.disjoint(list1, list2);

这篇关于检查一个列表是否包含另一个列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆