- List 的 equals() .首先比较两个列表大小是否相等。如果大小相等再逐个比较元素是否相等。
java
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
csharp
# Person未覆写了equals
Person per1 = new Person("name1");
Person per2 = new Person("name2");
List<Person> list1 = new ArrayList<Person>();
list1.add(per1);
list1.add(per2);
Person per3 = new Person("name1");
Person per4 = new Person("name2");
List<Person> list2 = new ArrayList<Person>();
list2.add(per3);
list2.add(per4);
System.out.println(list1.equals(list2)); // false
System.out.println(CollectionUtils.isEqualCollection(list1, list2)); //false
# Person加@Data覆写了equals
Person per1 = new Person("name1");
Person per2 = new Person("name2");
List<Person> list1 = new ArrayList<Person>();
list1.add(per1);
list1.add(per2);
Person per3 = new Person("name1");
Person per4 = new Person("name2");
List<Person> list2 = new ArrayList<Person>();
list2.add(per3);
list2.add(per4);
System.out.println(list1.equals(list2)); // true
System.out.println(CollectionUtils.isEqualCollection(list1, list2)); //true