zoukankan      html  css  js  c++  java
  • 集合:Collection接口(常用方法、遍历)

    1、Collection接口

    (1)特点

    •  没有直接的实现类,而是提供了子接口
    • 该接口提供了一系列常见的集合操作的方法,例如:增加、删除、查找

    (2)常见方法

    • add:单个添加
    • addAll:批量添加
    • remove:删除
    • removeAll:批量删除
    • contains:查找
    • containsAll:批量查找

    2、Collection接口的常用方法

    (1)添加:

    add

    @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println(collection);
        }
    [zhai, true, 123]

    addAll

    public class DateDemo {
        @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            Collection collection1=new ArrayList();
            collection1.add("2020年8月4日20:35:10");
            collection1.add("2020年8月4日20:35:30");
            collection.addAll(collection1);
            System.out.println("批量添加后的collection:"+collection);
        }
    }
    collection:[zhai, true, 123]
    批量添加后的collection:[zhai, true, 123, 2020年8月4日20:35:10, 2020年8月4日20:35:30]

    添加自定义类型的对象元素:

    @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add(new Student("zhai",12));
            collection.add(new Student("zhao",13));
            collection.add(new Student("zhang",14));
            collection.add(new Student("li",12));
            System.out.println(collection);
        }
    [Student{sname='zhai', age=12}, 
    Student{sname='zhao', age=13}, 
    Student{sname='zhang', age=14}, 
    Student{sname='li', age=12}]

    (2)删除

    remove

     @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            collection.remove(123);
            System.out.println("删除后:"+collection);
        }

    removeAll

      @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            Collection collection1=new ArrayList();
            collection1.add("2020年8月4日20:35:10");
            collection1.add("2020年8月4日20:35:30");
            collection.addAll(collection1);
            System.out.println("批量添加后的collection:"+collection);
            collection.removeAll(collection1);
            System.out.println("批量删除后:"+collection);
        }
    collection:[zhai, true, 123]
    批量添加后的collection:[zhai, true, 123, 2020年8月4日20:35:10, 2020年8月4日20:35:30]
    批量删除后:[zhai, true, 123]

    (3)查找

    contains

     @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            System.out.println(collection.contains("zhai"));
        }
    collection:[zhai, true, 123]
    true

    containsAll

     @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            Collection collection1=new ArrayList();
            collection1.add("2020年8月4日20:35:10");
            collection1.add("2020年8月4日20:35:30");
            collection.addAll(collection1);
            System.out.println("批量添加后的collection:"+collection);
            System.out.println(collection.containsAll(collection1));
        }
    collection:[zhai, true, 123]
    批量添加后的collection:[zhai, true, 123, 2020年8月4日20:35:10, 2020年8月4日20:35:30]
    true

    (4)获取元素个数

        @Test
        public void test1(){
            Collection collection=new ArrayList();//创建Collection接口对象
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            System.out.println(collection.size());
        }
    collection:[zhai, true, 123]
    3

    (5)清除

     @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println("collection:"+collection);
            collection.clear();
            System.out.println(collection);
            System.out.println(collection.size());
        }
    collection:[zhai, true, 123]
    []
    0

    (6)判断是否为空

    @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add("zhai");
            collection.add(true);
            collection.add(123);
            System.out.println(collection.isEmpty());
            System.out.println("collection:"+collection);
            collection.clear();
            System.out.println(collection);
            System.out.println(collection.size());
            System.out.println(collection.isEmpty());
        }
    false
    collection:[zhai, true, 123]
    []
    0
    true

    集合为空的时候返回true,否则返回false

    3、遍历

    (1)迭代器

    流程:

     特点:

    每次只能下移一位

    只能下移

    遍历方式:

    public class DateDemo {
        @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add(new Student("zhai",12));
            collection.add(new Student("zhao",13));
            collection.add(new Student("zhang",14));
            collection.add(new Student("li",12));
            Iterator iterator=collection.iterator();
            while (iterator.hasNext()){
                    System.out.println(iterator.next());
            }
        }
    }
    Student{sname='zhai', age=12}
    Student{sname='zhao', age=13}
    Student{sname='zhang', age=14}
    Student{sname='li', age=12}
    • 适合读取元素,进行添加操作
     @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add(new Student("zhai",12));
            collection.add(new Student("zhao",13));
            collection.add(new Student("zhang",14));
            collection.add(new Student("li",12));
            Iterator iterator=collection.iterator();
            while (iterator.hasNext()){
                collection.add(new Student("li",10));
                System.out.println(iterator.next());
            }
        }
    java.util.ConcurrentModificationException

    默认的元素个数是4,每次遍历的时候都会计算元素的个数,只要影响了实际的元素个数就会发生异常(底层存在一个变量存储默认值为0,在创建了迭代器对象之后该变量的值变为集合的大小,每次调用next都会验证当前集合的大小和变量值的大小,不相等会抛出异常),因此,进行添加、删除操作就会抛出异常。能够维护数据的安全性。

    修改没有影响元素的个数,不会抛出此异常

    一般迭代的时候是不会进行增加操作,删除的时候可以使用迭代器本身的remove方法

     @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add(new Student("zhai",12));
            collection.add(new Student("zhao",13));
            collection.add(new Student("zhang",14));
            collection.add(new Student("li",12));
            Iterator iterator=collection.iterator();
            while (iterator.hasNext()){
                Student next= (Student) iterator.next();
                if (next.getAge()<13){
                    iterator.remove();
                }
            }
            for (Object s:collection){
                System.out.println(s);
            }
        }

    (2)增强for

     @Test
        public void test1(){
            Collection collection=new ArrayList();
            collection.add(new Student("zhai",12));
            collection.add(new Student("zhao",13));
            collection.add(new Student("zhang",14));
            collection.add(new Student("li",12));
            for(Object student:collection){
                System.out.println(student);
            }
        }
    Student{sname='zhai', age=12}
    Student{sname='zhao', age=13}
    Student{sname='zhang', age=14}
    Student{sname='li', age=12}
    • 本质上是迭代器,只是语法简化了
  • 相关阅读:
    MegaCli 简易使用介绍
    【转】RAID 技术发展综述
    HOWTO For iSCSI-SCST && Gentoo HOWTO For iSCSI-SCST
    FC磁盘设备管理
    targetcli配置iSCSI
    NVMe协议1.3c(一) 概述
    NVMe概述
    Jenkins slave image
    ansible module
    Ansible Filter
  • 原文地址:https://www.cnblogs.com/zhai1997/p/13436119.html
Copyright © 2011-2022 走看看