学习过程中遇到的小问题,collecion集合的方法,自定义了一个person类,再代码中进行了实例化,再添加到集合中,结果就是我想要的,如下:
代码:
Person zs = new Person("zhangsan",18);
Collection collection = new ArrayList();
collection.add("ABC");
collection.add(zs);
collection.add(new Person("Laoli",16));
Collection c2 = new ArrayList();
c2.add(zs);
collection.retainAll(c2);
Iterator t1 = collection.iterator();
System.out.println(collection);
while(t1.hasNext()) {
System.err.println(t1.next());
}
结果:
Person [name=zhangsan, age=18]
[Person [name=zhangsan, age=18]]
但是,如果直接进行add就不可以,代码如下:
Collection collection = new ArrayList();
collection.add("ABC");
collection.add(new Person("zhangsan",18));
collection.add(new Person("Laoli",16));
Collection c2 = new ArrayList();
c2.add(new Person("zhangsan",18));
collection.retainAll(c2);
Iterator t1 = collection.iterator();
System.out.println(collection);
while(t1.hasNext()) {
System.err.println(t1.next());
}
结果:
[]
我能想到的是可能是因为new的缘故,两个不同对象的问题,但是我的疑问点就出现了
疑问点:jdk1.6就没问题,我用的1.8就这样,难道是1.8改了?
求那位大神详细解答一下,多谢!!
应为你的方法二中,创建了两个对象,这两个对象虽然值一样,但是其栈中的引用不同,但是list.retainAll使用本实体类中默认的equals进行判断,实体类在没有重写equals方法时,默认采用Object中equals方法,这个equals方法默认判断的是栈中的引用,所以这两个对象不相等,取交集就获取不到,我们只需要在实体类中重写equals方法即可(因为Sysetem.out和Sysetem.err执行顺序问题,下方代码同意采用Sysetem.out)
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class XmlUtils {
public static void main(String[] args) {
System.out.println("java版本"+System.getProperty("java.version"));
System.out.println("===============方法一=============");
{
Person zs = new Person("zhangsan",18);
Collection collection = new ArrayList();
collection.add("ABC");
collection.add(zs);
collection.add(new Person("Laoli",16));
Collection c2 = new ArrayList();
c2.add(zs);
collection.retainAll(c2);
Iterator t1 = collection.iterator();
System.out.println(collection);
while(t1.hasNext()) {
System.out.println(t1.next());
}
}
System.out.println("===============方法二=============");
{
Collection collection = new ArrayList();
collection.add("ABC");
collection.add(new Person("zhangsan",18));
collection.add(new Person("Laoli",16));
Collection c2 = new ArrayList();
c2.add(new Person("zhangsan",18));
collection.retainAll(c2);
Iterator t1 = collection.iterator();
System.out.println(collection);
while(t1.hasNext()) {
System.out.println(t1.next());
}
}
}
}
class Person{
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
return true;
}
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
我没用过jdk1.6,但是在1.8中你这肯定是不行的,比较引用类型时比较的是地址,你new了就说明是新对象地址不同比较结果当然是不相等.