单列集合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去判断是否包含的,源码:

相关推荐
pshdhx_albert4 小时前
AI agent实现打字机效果
java·http·ai编程
沉鱼.444 小时前
第十二届题目
java·前端·算法
赫瑞5 小时前
数据结构中的排列组合 —— Java实现
java·开发语言·数据结构
周末也要写八哥6 小时前
多进程和多线程的特点和区别
java·开发语言·jvm
惜茶7 小时前
vue+SpringBoot(前后端交互)
java·vue.js·spring boot
杰克尼8 小时前
springCloud_day07(MQ高级)
java·spring·spring cloud
NHuan^_^9 小时前
SpringBoot3 整合 SpringAI 实现ai助手(记忆)
java·人工智能·spring boot
Mr_Xuhhh9 小时前
从ArrayList到LinkedList:理解链表,掌握Java集合的另一种选择
java·数据结构·链表
错把套路当深情10 小时前
Java 全方向开发技术栈指南
java·开发语言
han_hanker10 小时前
springboot 一个请求的顺序解释
java·spring boot·后端