Check if one list contains element from the other

前端 未结 12 2633
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 20:45

I have two lists with different objects in them.

List list1;
List list2;

I want to check if element from list

12条回答
  •  佛祖请我去吃肉
    2020-11-30 21:33

    to make it faster, you can add a break; that way the loop will stop if found is set to true:

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

    If you would have maps in stead of lists with as keys the attributeSame, you could check faster for a value in one map if there is a corresponding value in the second map or not.

提交回复
热议问题