博客
关于我
Collection接口
阅读量:214 次
发布时间:2019-02-28

本文共 8370 字,大约阅读时间需要 27 分钟。

一、单列集合框架结构

|----Collection接口:单列集合,用来存储一个一个的对象

           |----List接口:存储有序的、可重复的数据。 -->“动态”数组
                    |----ArrayList、LinkedList、Vector
           |----Set接口:存储无序的、不可重复的数据 -->高中讲的“集合”
                    |----HashSet、LinkedHashSet、TreeSet

在这里插入图片描述

二、Collection接口中常用的方法

添加

add(Object obj)将元素obj添加到集合中

@Test    public void test1(){           Collection coll = new ArrayList();        coll.add("AA");        coll.add(123);        coll.add(new Date());        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:25:47 CST 2021]    }

addAll(Collection coll)将另一个集合中的所有元素添加到当前集合中

@Test    public void test1(){           Collection coll = new ArrayList();        coll.add("AA");        coll.add(123);        coll.add(new Date());        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:25:47 CST 2021]        System.out.println(coll.size());//3        Collection coll2 = new ArrayList();        coll2.add("sss");        coll2.add(456);        coll.addAll(coll2);        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:30:52 CST 2021, sss, 456]    }

获取有效元素的个数

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

@Test    public void test1(){           Collection coll = new ArrayList();        coll.add("AA");        coll.add(123);        coll.add(new Date());        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:25:47 CST 2021]        System.out.println(coll.size());//3    }

是否是空集合

boolean isEmpty()判断是否是空集合

@Test    public void test2(){           Collection coll3 = new ArrayList();        System.out.println(coll3.isEmpty());        //true    }
/**     * Returns true if this list contains no elements.     *     * @return true if this list contains no elements     */    public boolean isEmpty() {           return size == 0;    }

清空集合

void clear()清空集合中所有的元素

@Test    public void test2(){           Collection coll3 = new ArrayList();        coll3.add(111);        System.out.println(coll3.isEmpty());        //false        coll3.clear();        System.out.println(coll3.isEmpty());        //true    }

是否包含某个元素

boolean contains(Object obj)是通过元素的equals方法来判断是否是同一个对象,向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        System.out.println(coll4.contains(123));        //true        System.out.println(coll4.contains(new String("hello")));      //true    }

boolean containsAll(Collection c)也是调用元素的equals方法来比较的。拿两个集合的元素挨个比较。

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Collection coll5 = new ArrayList();        coll5.add(456);        coll5.add(false);        System.out.println(coll4.containsAll(coll5));       //true   }

删除

boolean remove(Object obj)通过元素的equals方法判断是否是要删除的那个元素。只会删除找到的第一个元素。

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        System.out.println(coll4);      //[123, 456, hello, false, Person{name='Tom', age=24}]        coll4.remove(123);        System.out.println(coll4);      //[456, hello, false, Person{name='Tom', age=24}]    }

boolean removeAll(Collection coll)从当前集合中移除集合coll中的所有元素

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Collection coll5 = new ArrayList();        coll5.add(456);        coll5.add(false);        System.out.println(coll4);      //[123, 456, hello, false, Person{name='Tom', age=24}]        coll4.removeAll(coll5);        System.out.println(coll4);      //[123, hello, Person{name='Tom', age=24}]    }

取两个集合的交集

boolean retainAll(Collection c)把交集的结果存在当前集合中,不影响集合c

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Collection coll5 = new ArrayList();        coll5.add(456);        coll5.add(789);        System.out.println(coll4);      //[123, 456, hello, false, Person{name='Tom', age=24}]        coll4.retainAll(coll5);        System.out.println(coll4);      //[456]        System.out.println(coll5);      //[456, 789]   }

集合是否相等

boolean equals(Object obj)比较两个集合是否相等

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Collection coll5 = new ArrayList();        coll5.add(123);        coll5.add(456);        coll5.add(new String("hello"));        coll5.add(false);        coll5.add(new Person("Tom",24));        System.out.println(coll4.equals(coll5));        //true,要注意集合元素是有序的    }

获取集合对象的哈希值

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

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        System.out.println(coll4.hashCode());       //964169686    }

转成对象数组

Object[] toArray()返回Object类型数组

@Test    public void test3(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Object[] arr = coll4.toArray();        for (int i = 0; i < arr.length; i++) {               System.out.println(arr[i]);        }    }

数组也可以转成集合,调用Arrays类的静态方法asList()

List
list = Arrays.asList(new String[]{ "AA", "BB", "CC"});

但是要注意的是下面这种情况

List
list = Arrays.asList(new int[]{ 123,456});System.out.println(list); //[[I@22927a81]

如果是int类型的数组,输出的结果是一个一维数组的地址,元素为int类型。即相当于把整体结构也就是可变形参new int[]{123,456}当成了一个元素,而不是把数组里面的int类型的值当成多个元素。

应该修改为

List list = Arrays.asList(123,456);System.out.println(list);		//[123, 456]//或者使用Integer包装类List list = Arrays.asList(new Integer[]{   123,456});

三、遍历(Iterator迭代器)

  • iterator()返回Iterator接口实例对象,即迭代器对象,用于集合遍历。
  • Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。
  • Collection接口继承了java.lang.Iterable接口,该接口有一个iterator()方法,那么所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象。
  • Iterator 仅用于遍历集合,Iterator 本身并不提供承装对象的能力。如果需要创建Iterator 对象,则必须有一个被迭代的集合。
  • 集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
@Test    public void IteratorTest(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Iterator iterator = coll4.iterator();        while (iterator.hasNext()){               System.out.println(iterator.next());        }    }

迭代器执行原理

//hasNext()方法判断是否还有下一个元素        while (iterator.hasNext()){           	//next()指针下移,然后将下移以后集合位置上的元素返回            System.out.println(iterator.next());        }

在这里插入图片描述

Iterator接口remove()方法,可以删除集合的元素,但是是遍历过程中通过迭代器对象remove方 法,不是集合对象的remove方法。

@Test    public void IteratorTest(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        Iterator iterator = coll4.iterator();        while (iterator.hasNext()){               Object obj = iterator.next();            if ("Tom".equals(obj)){        //删除集合中Tom数据                iterator.remove();            }        }        iterator = coll4.iterator();      //需要重新获得一个迭代器从头开始遍历        while (iterator.hasNext()){               System.out.println(iterator.next());        }    }}

四、foreach循环遍历集合元素

  • 遍历操作不需获取Collection或数组的长度,无需使用索引访问元素。
  • 遍历集合的底层调用Iterator完成操作。
  • foreach还可以用来遍历数组。
@Test    public void ForeachTest(){           Collection coll4 = new ArrayList();        coll4.add(123);        coll4.add(456);        coll4.add(new String("hello"));        coll4.add(false);        coll4.add(new Person("Tom",24));        //for(集合元素的类型 局部变量 : 集合对象)        for(Object obj : coll4){               System.out.println(obj);        }    }

转载地址:http://cnpi.baihongyu.com/

你可能感兴趣的文章
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>
NIFI大数据进阶_NIFI监控功能实际操作_Summary查看系统和处理器运行情况_viewDataProvenance查看_---大数据之Nifi工作笔记0026
查看>>
NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
查看>>
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_01---大数据之Nifi工作笔记0033
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_说明操作步骤---大数据之Nifi工作笔记0028
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南002---大数据之Nifi工作笔记0069
查看>>
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>