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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?