• 48_Collection集合


    第48章 Collection集合

    作者:张子默

    一、集合概述

    集合是Java中提供的一种容器,可以用来存储多个数据。

    集合和数组的区别:

    • 数组的长度是固定的。集合的长度是可变的。
    • 数组中存储的是同一类型的元素,可以存储基本类型数值。集合存储的都是对象。而且对象的类型可以不一致。在开发中一般当对象多的时候,使用集合进行存储。

    二、集合框架

    Java SE提供了满足各种需求的API,在使用这些API前,先了解其继承与接口操作架构,才能了解何时采用哪个类,以及类之间如何彼此合作,从而达到灵活应用。

    集合按照其存储结构可以分为两大类,分别是单列集合java.util.Collection和双列集合java.util.Map,这里我们主要讲述的是Collection集合。

    • Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是java.util.Listjava.util.Set。其中,List的特点是元素有序、元素可重复。Set的特点是元素无序,而且不可重复。List接口的主要实现类有java.util.ArrayListjava.util.LinkedListSet接口的主要实现类有java.util.HashSetjava.util.TreeSet

    从上述的描述可以看出JDK提供了丰富的集合类库,为了便于更好地系统性学习,接下来通过一张图来描述整个集合类的继承体系。

    在这里插入图片描述

    集合本身就是一个工具,它存放在java.util包中。在Collection接口定义着单列集合框架中最最共性的内容。

    三、Collection集合的常用功能

    Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:

    • public boolean add(E e) :把给定的对象添加到当前集合中 。
    • public void clear() :清空集合中所有的元素。
    • public boolean remove(E e) : 把给定的对象在当前集合中删除。
    • public boolean contains(E e) : 判断当前集合中是否包含给定的对象。
    • public boolean isEmpty() : 判断当前集合是否为空。
    • public int size() : 返回集合中元素的个数。
    • public Object[] toArray() : 把集合中的元素,存储到数组中。

    方法演示:

    import java.util.ArrayList;
    import java.util.Collection;
    public class Demo1Collection {
        public static void main(String[] args) {
            // 创建集合对象
            // 使用多态形式
            Collection<String> coll = new ArrayList<String>();
            // 使用方法
            // 添加功能 boolean add(String s)
            coll.add("小李广");
            coll.add("扫地僧");
            coll.add("石破天");
            System.out.println(coll);
            // boolean contains(E e) 判断o是否在集合中存在
            System.out.println("判断 扫地僧 是否在集合中"+coll.contains("扫地僧"));
            //boolean remove(E e) 删除在集合中的o元素
            System.out.println("删除石破天:"+coll.remove("石破天"));
            System.out.println("操作之后集合中元素:"+coll);
            // size() 集合中有几个元素
            System.out.println("集合中有"+coll.size()+"个元素");
            // Object[] toArray()转换成一个Object数组
            Object[] objects = coll.toArray();
            // 遍历数组
            for (int i = 0; i < objects.length; i++) {
                System.out.println(objects[i]);
            }
            // void clear() 清空集合
            coll.clear();
            System.out.println("集合中内容为:"+coll);
            // boolean isEmpty() 判断是否为空
            System.out.println(coll.isEmpty());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    tips:有关Collection中的方法尚不止这些,其他方法可查看API文档了解。

    四、实例

    package com.zzm.day13.demo01;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    
    /**
     * 用途:
     * 时间:2021/6/29 23:13
     * 创建人:张子默
     */
    
    /*
    java.util.Collection接口
        所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
        任意的单列集合都可以使用Collection接口中的方法
    
    共性的方法:
        public boolean add(E e):把给定的对象添加到当前集合中。
        public void clear():清空集合中所有的元素。
        public boolean remove(E e):把给定的对象在当前集合中删除。
        public boolean contains(E e):判断当前集合中是否包含给定的对象。
        public boolean isEmpty():判断当前集合是否为空。
        public int size():返回集合中元素的个数。
        public Object[] toArray():把集合中的元素存储到数组中。
     */
    public class Demo01Collection {
    
        public static void main(String[] args) {
            // 创建集合对象可以使用多态
            // Collection<String> coll = new ArrayList<>();
            Collection<String> coll = new HashSet<>();
            System.out.println(coll); // 重写了toString方法 []
    
            /*
             public boolean add(E e):把给定的对象添加到当前集合中。
             返回值是一个boolean值,一般都返回true,所以不用接收
             */
            boolean b1 = coll.add("张三");
            System.out.println("b1:" + b1); // b1:true
            System.out.println(coll); // [张三]
            coll.add("李四");
            coll.add("王五");
            coll.add("赵六");
            coll.add("田七");
            System.out.println(coll); // [李四, 张三, 王五, 赵六, 田七]
    
            /*
            public boolean remove(E e):把给定的对象在当前集合中删除。
            返回值是一个boolean值:
                集合中存在元素,删除元素,返回true
                集合中不存在元素,删除失败,返回false
             */
            boolean b2 = coll.remove("赵六");
            System.out.println("b2:" + b2); // b2:true
    
            boolean b3 = coll.remove("赵六");
            System.out.println("b3:" + b3); // b3:false
            System.out.println(coll); // [李四, 张三, 王五, 田七]
    
            /*
            public boolean contains(E e):判断当前集合中是否包含给定的对象。
            包含返回true
            不包含返回false
             */
            boolean b4 = coll.contains("李四");
            System.out.println("b4:" + b4); // b4:true
    
            boolean b5 = coll.contains("赵四");
            System.out.println("b5:" + b5); // b5:false
    
            // public boolean isEmpty():判断当前集合是否为空。集合为空返回true,集合不为空返回false
            boolean b6 = coll.isEmpty();
            System.out.println("b6:" + b6); // b6:false
    
            // public int size():返回集合中元素的个数。
            int size = coll.size();
            System.out.println("size:" + size); // size:4
            
            // public Object[] toArray():把集合中的元素存储到数组中。
            Object[] arr = coll.toArray();
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
    
            // public void clear():清空集合中所有的元素。但是不能删除集合,集合还存在
            coll.clear();
            System.out.println(coll); // []
            System.out.println(coll.isEmpty()); // true
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
  • 相关阅读:
    什么是安规电容、X电容、Y电容?
    C1N短网址 - 是如何做到行业领先的
    代码大全阅读随笔(十二)完结
    中级软件设计师考试(软考中级)标准化和软件知识产权
    C语言----数组
    如果不封车,坚持冬天骑行应该注意些什么?
    Rabbitmq入门教程
    vue : 无法加载文件 C:\XXX\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。
    三位数的IMU长什么样?二位数的呢?不要钱的呢?| 为FishBot配置IMU惯性测量单元
    vue制作自己的组件库(仿ElementUI)
  • 原文地址:https://blog.csdn.net/a1448824839/article/details/125612992