两个List比较内容是否一样

实现

一段进行对两个list进行比较的代码。

/**
     * 首先进行入参检查防止出现空指针异常
     * 如果两个参数都为空,则返回true
     * 如果有一项为空,则返回false
     * 接着对第一个list进行遍历,如果某一项第二个list里面没有,则返回false
     * 还要再将两个list反过来比较,因为可能一个list是两一个list的子集
     * 如果成功遍历结束,返回true
     * @param l0
     * @param l1
     * @return
     */
public static boolean isListEqual(List l0, List l1){
        if (l0 == l1)
            return true;
        if (l0 == null && l1 == null)
            return true;
        if (l0 == null || l1 == null)
            return false;
        if (l0.size() != l1.size())
            return false;
        for (Object o : l0) {
            if (!l1.contains(o))
                return false;
        }
        for (Object o : l1) {
            if (!l0.contains(o))
                return false;
        }
        return true;
    }
  • 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

原理

这种比较可以成功比较两个内容相同但是元素顺序不同的list。 
比较关键的是使用了List.contains()方法,这个方法是个接口。以ArrayList为例,看一下ArrayList里的实现:

/**
     * 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&nbsp;?&nbsp;e==null&nbsp;:&nbsp;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&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;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;
    }
  • 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

contains()返回的是indexOf(Object o)是否大于0的boolean值。indexOf(Object o)返回的结果是目标对象的下标值。 
elementData是ArrayList里的Object的数组,elementData[]的声明(为什么将类型定为transient 不太明白):

// non-private to simplify nested class access
transient Object[] elementData; 
  • 1
  • 2

首先看indexOf()方法,当目标Object为空时,返回elementData中第一个为空元素的下标。否则如果目标Object不为空,调用Object的equals()方法,返回第一个相等元素的下标。 
当上述两种情况都能找到相应元素时,下标肯定都是大于等于0的,如果没找到的话返回-1。 
因此当contains()只需判断indexOf()的返回值是否大于等于0就可以判断该list是否包含目标元素。

问题

但是这种简单的判断两个list内容是否一样的代码有一个问题,因为最终的元素的比较依赖于equals()方法。当List里的元素没有重写equals()方法时,这个判断的结果是不准确的。

class AA{

    public String s;

    public AA(String s){
        this.s = s;
    }
}

ArrayList l0 = new ArrayList();
l0.add(new AA("aaa"));
l0.add(new AA("bbb"));

ArrayList l1 = new ArrayList();
l1.add(new AA("aaa"));
l1.add(new AA("bbb"));

System.out.println(isListEqual(l0, l1));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

上述代码应该返回的结果为true,但实际返回的结果为false。 
这里写图片描述

class AA{

    public String s;

    public AA(String s){
        this.s = s;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == null && obj == null)
            return true;
        if (this == null || obj == null)
            return false;
        if (obj.getClass() != this.getClass())
            return false;
        AA a = (AA) obj;
        if (this.s.equals(a.s))
            return true;
        return false;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

为AA类重写equals()后再次运行返回结果正常。 
这里写图片描述

结论

1.凡涉及到对象的比较是否相同时,确保实现了equals()方法,以免发生意外。 
2.该判断方法的代码只针对包含了实现了equals的对象的list。

未解决的问题

ArrayList里的elementData字段为什么要加transient 关键字

猜你喜欢

转载自blog.csdn.net/sky786905664/article/details/79877580