QWQyun 2020-05-31 22:29 采纳率: 100%
浏览 103
已采纳

collecion集合的方法没有获取想要的结果,求大神解答!

学习过程中遇到的小问题,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改了?
求那位大神详细解答一下,多谢!!

  • 写回答

2条回答 默认 最新

  • 人到中年就秃头 2020-06-02 10:31
    关注

    应为你的方法二中,创建了两个对象,这两个对象虽然值一样,但是其栈中的引用不同,但是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 +
                    '}';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?