• 11-2 集合之Collection接口


    Collection接口

    • Collection 接口是 List、Set 和 Queue 接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 和 Queue 集合。
    • JDK不提供此接口的任何直接实现,而是提供更具体的子接口(如:Set和List)实现。
    • 在 Java5 之前,Java 集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object 类型处理;从 JDK 5.0 增加了泛型以后,Java 集合可以记住容器中对象的数据类型。
    1. 添加
      add(Object obj)
      addAll(Collection coll)
    2. 获取有效元素的个数
      int size()
    3. 清空集合
      void clear()
    4. 是否是空集合
      boolean isEmpty()
    5. 是否包含某个元素
      boolean contains(Object obj):是通过元素的equals方法来判断是否是同一个对象
      boolean containsAll(Collection c):也是调用元素的equals方法来比较的。拿两个集合的元素挨个比较。
    6. 删除
      boolean remove(Object obj) :通过元素的equals方法判断是否是
      要删除的那个元素。只会删除找到的第一个元素
      boolean removeAll(Collection coll):取当前集合的差集
    7. 取两个集合的交集
      boolean retainAll(Collection c):把交集的结果存在当前集合中,不影响c
    8. 集合是否相等
      boolean equals(Object obj)
    9. 转成对象数组
      Object[] toArray()
    10. 获取集合对象的哈希值
      hashCode()
    11. 遍历
      iterator():返回迭代器对象,用于集合遍历

    在这里插入图片描述

    Collection coll = new ArrayList();
    
    • 1

    add(Object e):将元素e添加到集合coll中

    //add(Object e):将元素e添加到集合coll中
            coll.add("AA");
            coll.add("BB");
            coll.add(123);  //自动装箱
            coll.add(new Date());
    
    • 1
    • 2
    • 3
    • 4
    • 5

    size():获取添加的元素的个数

     //size():获取添加的元素的个数
            System.out.println(coll.size());    //4
    
    • 1
    • 2

    addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中

    //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
            Collection coll1 = new ArrayList();
            coll1.add(456);
            coll1.add("CC");
            coll.addAll(coll1);
    
            System.out.println(coll.size());    //6
            System.out.println(coll);//[AA, BB, 123, Wed Jul 20 08:38:54 CST 2022, 456, CC]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    clear():清空集合元素

    //clear():清空集合元素
            coll.clear();
    
    • 1
    • 2

    isEmpty():判断当前集合是否为空

    //isEmpty():判断当前集合是否为空
            System.out.println(coll.isEmpty());
    
    • 1
    • 2

    在这里插入图片描述

    Person类

    import java.util.Objects;
    
    public class Person {
    
        private String name;
        private int age;
    
        public Person() {
            super();
        }
    
        public Person(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;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        @Override
        public boolean equals(Object o) {
            System.out.println("Person equals()....");
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Person person = (Person) o;
            return age == person.age &&
                    Objects.equals(name, person.name);
        }
    
    //    @Override
    //    public int hashCode() {
    //
    //        return Objects.hash(name, age);
    //    }
    }
    
    
    • 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

    contains(Object obj):判断当前集合中是否包含obj

    我们在判断时会调用obj对象所在类的equals()。

            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
    
    //        Person p = new Person("Jerry",20);
    //        coll.add(p);
            coll.add(new Person("Jerry",20));
    
            coll.add(new String("Tom"));
            coll.add(false);
    
            //1.contains(Object obj):判断当前集合中是否包含obj
            //我们在判断时会调用obj对象所在类的equals()。
            boolean contains = coll.contains(123);
            System.out.println(contains);
            System.out.println(coll.contains(new String("Tam")));//true
    //        System.out.println(coll.contains(p));//true
            System.out.println(coll.contains(new Person("Jerry",20)));//false -->true(重写equals)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。

    //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
            Collection coll1 = Arrays.asList(123,4567);
            System.out.println(coll.containsAll(coll1));
    
    • 1
    • 2
    • 3

    结论:
    向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().

    remove(Object obj):从当前集合中移除obj元素。

    //3.remove(Object obj):从当前集合中移除obj元素。
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            coll.remove(1234);
            System.out.println(coll);
    
            coll.remove(new Person("Jerry",20));
            System.out.println(coll);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素。

    //4. removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素。
            Collection coll1 = Arrays.asList(123,4567);
            coll.removeAll(coll1);
            System.out.println(coll);
    
    • 1
    • 2
    • 3
    • 4

    retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合

            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
    //        Collection coll1 = Arrays.asList(123,456,789);
    //        coll.retainAll(coll1);
    //        System.out.println(coll);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。

     //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。
            Collection coll1 = new ArrayList();
            coll1.add(456);
            coll1.add(123);
            coll1.add(new Person("Jerry",20));
            coll1.add(new String("Tom"));
            coll1.add(false);
    
            System.out.println(coll.equals(coll1));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    hashCode():返回当前对象的哈希值

    Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            //7.hashCode():返回当前对象的哈希值
            System.out.println(coll.hashCode());//-1200490100
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    集合 —>数组:toArray()

    		//8.集合 --->数组:toArray()
            Object[] arr = coll.toArray();
            for(int i = 0;i < arr.length;i++){
                System.out.println(arr[i]);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    拓展:数组 —>集合:调用Arrays类的静态方法asList()

    //拓展:数组 --->集合:调用Arrays类的静态方法asList()
            List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
            System.out.println(list);
    
            List arr1 = Arrays.asList(123, 456);
            System.out.println(arr1);//[123, 456]
    
            List arr2 = Arrays.asList(new int[]{123, 456});
            System.out.println(arr2.size());//1
    
            List arr3 = Arrays.asList(new Integer[]{123, 456});
            System.out.println(arr3.size());//2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试

    完整代码:

    public class CollectionTest {
        @Test
        public void test1(){
            Collection coll = new ArrayList();
    
            //add(Object e):将元素e添加到集合coll中
            coll.add("AA");
            coll.add("BB");
            coll.add(123);  //自动装箱
            coll.add(new Date());
    
            //size():获取添加的元素的个数
            System.out.println(coll.size());    //4
    
            //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
            Collection coll1 = new ArrayList();
            coll1.add(456);
            coll1.add("CC");
            coll.addAll(coll1);
    
            System.out.println(coll.size());    //6
            System.out.println(coll);//[AA, BB, 123, Wed Jul 20 08:38:54 CST 2022, 456, CC]
    
            //clear():清空集合元素
            coll.clear();
    
            //isEmpty():判断当前集合是否为空
            System.out.println(coll.isEmpty());
        }
    
    
        @Test
        public void test2(){
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
    
    //        Person p = new Person("Jerry",20);
    //        coll.add(p);
            coll.add(new Person("Jerry",20));
    
            coll.add(new String("Tom"));
            coll.add(false);
    
            //1.contains(Object obj):判断当前集合中是否包含obj
            //我们在判断时会调用obj对象所在类的equals()。
            boolean contains = coll.contains(123);
            System.out.println(contains);
            System.out.println(coll.contains(new String("Tam")));//true
    //        System.out.println(coll.contains(p));//true
            System.out.println(coll.contains(new Person("Jerry",20)));//false -->true
    
            System.out.println("------------------------");
            //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
            Collection coll1 = Arrays.asList(123,4567);
            System.out.println(coll.containsAll(coll1));
        }
    
    
        @Test
        public void test3(){
            //3.remove(Object obj):从当前集合中移除obj元素。
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            coll.remove(1234);
            System.out.println(coll);
    
            coll.remove(new Person("Jerry",20));
            System.out.println(coll);
    
            //4. removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素。
            Collection coll1 = Arrays.asList(123,4567);
            coll.removeAll(coll1);
            System.out.println(coll);
        }
    
        @Test
        public void test4(){
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
    //        Collection coll1 = Arrays.asList(123,456,789);
    //        coll.retainAll(coll1);
    //        System.out.println(coll);
    
            //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。
            Collection coll1 = new ArrayList();
            coll1.add(456);
            coll1.add(123);
            coll1.add(new Person("Jerry",20));
            coll1.add(new String("Tom"));
            coll1.add(false);
    
            System.out.println(coll.equals(coll1));
        }
    
        @Test
        public void test5(){
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            //7.hashCode():返回当前对象的哈希值
            System.out.println(coll.hashCode());//-1200490100
    
            //8.集合 --->数组:toArray()
            Object[] arr = coll.toArray();
            for(int i = 0;i < arr.length;i++){
                System.out.println(arr[i]);
            }
    
            //拓展:数组 --->集合:调用Arrays类的静态方法asList()
            List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
            System.out.println(list);
    
            List arr1 = Arrays.asList(123, 456);
            System.out.println(arr1);//[123, 456]
    
            List arr2 = Arrays.asList(new int[]{123, 456});
            System.out.println(arr2.size());//1
    
            List arr3 = Arrays.asList(new Integer[]{123, 456});
            System.out.println(arr3.size());//2
    
            //9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
        }
    }
    
    
    • 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
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141

  • 相关阅读:
    QT实现人脸识别
    【Linux】软交换机Open vSwitch的移植
    计算机毕业设计Java海康物流(源码+系统+mysql数据库+lw文档)
    Nature发布:ChatGPT 帮助我进行学术写作的三种方式
    IVIEW Table/Div 自适应高度
    Mac 环境下 Java JDK 的安装与环境变量配置详解(已完美解决)
    python lower函数用法
    Java 类和对象 详解+通俗易懂
    Leetcode71简化路径
    SpringBean的实例化
  • 原文地址:https://blog.csdn.net/qq_44774198/article/details/125885259