JAVA - 集合 -- contains,remove方法详解(包含底层源码分析)

contains方法

boolean contains(Object o)
如果此集合包含指定的元素,则返回 true 。

代码1演示
public class CollectionTest01 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        String s1 = new String("唐三");
        String s2 = new String("小舞");
        c.add(s1);
        c.add(s2);
        String x = new String("唐三");
        System.out.println(c.contains(x));
        //打印true
    }
}
分析

为什么会相等呢?难道比较的是内容相等吗?这里先不做回答,请看下面范例

代码2演示
public class CollectionTest01 {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<>();
        Student s1 = new Student("戴沐白");
        Student s2 = new Student("朱竹清");
        c.add(s1);
        c.add(s2);
        Student x = new Student("戴沐白");
        System.out.println(c.contains(x));
        //打印false
    }
}
class Student{
    private String name;
    Student(){}
    Student(String name){
        this.name = name;
    }
}
分析

出现了两个不一样的结果,根据我们前面的分析,难道不是比较的内容相同吗?此时来看看contains的底层源码

public boolean contains(Object o) {
	return indexOf(o) >= 0;
}
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;
}

contains方法底层调用的是equals方法。再来分析第二个代码,我们会发现Student引用类型并没有重写equals方法,如果没重写那么就会调用父类的equals方法,它的父类是Object类,跟进Object类的equals方法。

public boolean equals(Object obj) {
        return (this == obj);
    }

此时发现比较的是内存地址。

代码3演示
public class CollectionTest01 {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<>();
        Student s1 = new Student("唐三");
        Student s2 = new Student("小舞");
        c.add(s1);
        c.add(s2);
        Student x = new Student("唐三");
        //重写equals方法之后
        System.out.println(c.contains(x));
        //true
    }
}
class Student{
    private String name;
    Student(){}
    Student(String name){
        this.name = name;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || !(o instanceof Student)) {
            return false;
        }
        //如果名字一样,表示同一个人。 不再比较内存地址。
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }
}

当重写了equals方法后,就会得到我们想要的结果

remove方法

remove(Object o)
从该集合中删除指定元素的单个实例(如果存在)。

代码1演示
public class CollectionTest01 {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<>();
        Student s1 = new Student("唐银");
        Student s2 = new Student("小舞");
        c.add(s1);
        c.add(s2);
        Student x = new Student("唐银");
        //重写equals方法之前
        System.out.println(c.remove(x));
        //false
    }
}
class Student{
    private String name;
    Student(){}
    Student(String name){
        this.name = name;
    }
}
代码2演示
public class CollectionTest01 {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<>();
        Student s1 = new Student("戴沐白");
        Student s2 = new Student("朱竹清");
        c.add(s1);
        c.add(s2);
        Student x = new Student("戴沐白");
        //重写equals方法之后
        System.out.println(c.remove(x));
        //true
    }
}
class Student{
    private String name;
    Student(){}
    Student(String name){
        this.name = name;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || !(o instanceof Student)) {
            return false;
        }
        //如果名字一样,表示的是同一个人。 不再比较内存地址。
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }
}
remove底层源码
public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
}

在删除的时候也会调用equals方法去比较内容是否相同,如果没重写equals方法,将会调用其父类(Object)的equals方法去比较内存地址是否相同,这样可能不会删除我们想删除的对象。

总结

加入集合中的类型一定要重写equals方法。不重写比较的是内存地址,重写了比较的是内容。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值