单列集合Collection常用api

集合体系结构

Collection

Collection是单列集合的祖宗接口,它的功能是全部单列集合都可以继承使用的。

java 复制代码
 public static void main(String[] args) {
        //TODO Collection类 所有集合的接口
/*
        public boolean add(E e)             添加
        public void clear()                 清空
        public boolean remove(E e)          删除
        public boolean contains(Object obj) 判断是否包含
        public boolean isEmpty()            判断是否为空
        public int size()                   集合长度


       注意点:
        Collection是一个接口,我们不能直接创建他的对象。
        所以,只能创建他实现类的对象。
        实现类:ArrayList
*/

        Collection<String> coll = new ArrayList<>();

//1.添加元素
//细节1:如果我们要往List系列集合中添加数据,那么方法永远返回true,因为List系列的是允许元素重复的。
//细节2:如果我们要往Set系列集合中添加数据,如果当前要添加元素不存在,方法返回true,表示添加成功。
        //如果当前要添加的元素已经存在,方法返回false,表示添加失败。
        //因为Set系列的集合不允许重复。
        coll.add("aaa");
        coll.add("bbb");
        coll.add("ccc");
        System.out.println(coll);

        //2.清空
        //coll.clear();

        //3.删除
        //细节1:因为Collection里面定义的是共性的方法,所以此时不能通过索引进行删除。只能通过元素的对象进行删除。
        //细节2:方法会有一个布尔类型的返回值,删除成功返回true,删除失败返回false
        //如果要删除的元素不存在,就会删除失败。
        System.out.println(coll.remove("aaa"));
        //不存在
        System.out.println(coll.remove("abc"));
        System.out.println(coll);

        //4.判断元素是否包含
        //细节:底层是依赖equals方法进行判断是否存在的。
        //所以,如果集合中存储的是自定义对象,也想通过contains方法来判断是否包含,那么在javabean类中,一定要重写equals方法。
        boolean result1 = coll.contains("bbb");
        System.out.println(result1);

        //5.判断集合是否为空
        boolean result2 = coll.isEmpty();
        System.out.println(result2);//false

        //6.获取集合的长度
        coll.add("ddd");
        int size = coll.size();
        System.out.println(size);//3
    }
java 复制代码
public static void main(String[] args) {
        //1.创建集合的对象
        Collection<Student> coll = new ArrayList<>();

        //2.创建三个学生对象
        Student s1 = new Student("zhangsan",23);
        Student s2 = new Student("lisi",24);
        Student s3 = new Student("wangwu",25);

        //3.把学生对象添加到集合当中
        coll.add(s1);
        coll.add(s2);
        coll.add(s3);

        //4.判断集合中某一个学生对象是否包含
        Student s4 = new Student("zhangsan",23);
        //因为contains方法在底层依赖equals方法判断对象是否一致的。
        //如果存的是自定义对象,没有重写equals方法,那么默认使用Object类中的equals方法进行判断,而Object类中equals方法,依赖地址值进行判断。
        //需求:如果同姓名和同年龄,就认为是同一个学生。
        //所以,需要在自定义的Javabean类中,重写equals方法就可以了。
        System.out.println(coll.contains(s4));
    }
}


class Student {
    private String name;
    private int age;


    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //alt+insert快速生成 equals方法
    @Override
    public boolean equals(Object o) {
        //地址值相等直接返回true
        if (this == o) return true;
        //o为null或者 getClass不等于o直接返回false
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        //判断
        return age == student.age && Objects.equals(name, student.name);
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }
}

tips:查看Collection contains方法实现:

选中contains,右键选择Go To------》Implementation,找到相应的实现类(如ArrayList)。

可见底层是通过equals去判断是否包含的,源码:

相关推荐
Wyc724093 分钟前
Maven
java·数据库·maven
程序猿小D6 分钟前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
木头没有瓜1 小时前
idea离线安装插件
java·ide·intellij-idea
llwszx2 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
述雾学java2 小时前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
保持学习ing2 小时前
苍穹外卖day3--公共字段填充+新增菜品
java·阿里云·实战·springboot·前后端·外卖项目·阿里云文件存储
77qqqiqi2 小时前
正则表达式
java·后端·正则表达式
厦门德仔2 小时前
【WPF】WPF(样式)
android·java·wpf
大春儿的试验田2 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
Gappsong8743 小时前
【Linux学习】Linux安装并配置Redis
java·linux·运维·网络安全