java中两个list互相比较



java的应该是有个类可以专门实现:

package test;

import java.util.ArrayList;
import java.util.List;


public class Test {
public static void main(String[] args) {


List list1=new ArrayList();
List list2=new ArrayList();
List list3=new ArrayList();

UserInfo user=new UserInfo("name1",12);
UserInfo user1=new UserInfo("name2",12);
UserInfo user2=new UserInfo("name3",12);
UserInfo user3=new UserInfo("name4",12);

list1.add(user);
list1.add(user1);
list1.add(user2);
list1.add(user3);

list2.add(user);
list2.add(user);

for (int i = 0; i < list2.size(); i++) {
if(list1.containsAll(list2)){
list1.removeAll(list2);
System.err.println(list1.size());
}

}

}

}

class UserInfo{
public UserInfo(){}
public UserInfo(String name,int age){
this.userAge=age;
this.userName=name;

}

public String userName;
public int userAge;

public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

}

上边的可以,利用了2个方法:if(list1.containsAll(list2)){list1.removeAll(list2);}先判断是否包含,如果包含那么移除。



你可能感兴趣的:(java)