java两个list中保存bean对象,找出其中某一属性不同的元素

在java中运用List集合保存对象,如果想找到两个list中不同的部分,可以用ArrayList的contains方法,遍历每一个对象,判断是否是相等的,如下:

public static void getUncontain(List<String> list1, List<String> list2){
        for(String str1 : list1){
            if(!list2.contains(str1)){
                // 打印出list2没有b,d
                System.out.println("ArrayList2里没有的是==>" + str1);
            }
        }
        for(String str2 : list2){
            if(!list1.contains(str2)){
                // 打印出list1没有f,g
                System.out.println("ArrayList1里没有的是==>" + str2);
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

但是List中保存的是java中定义的bean对象呢? 
如:

public class person {

    private int id;
    private String name;



    public person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

此时如果还用contains方法判断由于是对象,每一次都不相同。

那该怎么筛选出两个list中不同的部分呢?

  • 1.这个方法比较笨,但是也是最容易想到的,就是用一个新的list,来记录两个list之间相同的地方,然后每个list将相同的部分去掉:
private static void getDifferentPart1(List<person> l1, List<person> l2) {
        List<person> newList=new ArrayList<>();
        List<person> diffeList=new ArrayList<>();
        for(person a : l1){
            for(person b:l2){
                if(b.getName().equals(a.getName())){
                    newList.add(a);//保存相同的部分。
                }
            }
        }
        for(person a:l2){
            int flag=0;
            for (person person : newList) {
                if(person.getName().equals(a.getName())){
                    flag=1;//相同的部分
                }
            }
            if(flag==0){
                diffeList.add(a);
            }
        }
        for (person person : diffeList) {
            System.out.println("diffeList:"+person.toString());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 2.第二种方法新手不容易想到,就是重写bean里面的equals方法:
public class person {

    private int id;
    private String name;



    public person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "id:"+id+","+"name:"+name+" ";
    }
    @Override
    public boolean equals(Object obj) {
        if(name.equals(((person)obj).getName()))
            return true;//这里以name为判定标准。
        else {
            return false;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

然后此时就可以用contains去比较对象:

private static void getDifferentPart2(List<person> l1, List<person> l2) {
        for(person a : l1){
            if(l2.contains(a)){
                System.out.println("ok!!!");
            }
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其实你看contains方法源码的话,会发现它底层就是用equals方法实现的,

contains方法中直接调用indexOf方法,indexOf方法中采用equals方法判断

/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}

/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

猜你喜欢

转载自blog.csdn.net/wang1988081309/article/details/78655737
今日推荐