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

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

单列集合框架结构

在Java集合框架中,集合是用来存储一系列对象的容器。集合可以分为两大类:单列集合多列集合。单列集合通过Collection接口定义,主要用于存储单个对象。其中,最常用的实现类包括ArrayList、LinkedList和Vector等。

List接口

List接口定义了一个有序、可重复的数据存储方式,类似于动态数组。它的主要特点包括:

  • 有序:元素的插入和排列顺序是有意义的。
  • 可重复:允许相同的元素多次出现。

典型实现类:

  • ArrayList:基于动态数组实现,查询效率较高。
  • LinkedList:基于双向链表实现,插入和删除效率较低,但迭代效率高。
  • Vector:与ArrayList类似,但方法命名为camelCase,已被ArrayList取代。

Set接口

Set接口定义了一个无序、不可重复的数据存储方式。它的主要特点包括:

  • 无序:元素的排列顺序无关紧要。
  • 不可重复:集合中不能包含相同的对象。

典型实现类:

  • HashSet:基于哈希表实现,查询效率高,缺少顺序。
  • LinkedHashSet:基于链表和哈希表结合实现,既能保持元素的无序性,又能保持元素的可重用性。
  • TreeSet:基于树状结构实现,能够按一定顺序排列元素。

Collection接口中常用的方法

Collection接口提供了一系列基本操作方法,主要包括以下几个方面:

1. 添加元素

  • add(Object obj):将元素obj添加到集合中。
    Collection coll = new ArrayList();coll.add("AA");coll.add(123);coll.add(new Date());
  • addAll(Collection coll):将另一个集合中的所有元素添加到当前集合中。
    Collection coll = new ArrayList();coll.add("AA");coll.add(123);coll.add(new Date());coll.addAll(new ArrayList());

2. 获取元素个数

  • size():返回集合中已添加的元素个数。
    Collection coll = new ArrayList();coll.add("AA");coll.add(123);coll.add(new Date());System.out.println(coll.size()); // 3

3. 判断是否为空

  • isEmpty():返回true,如果集合中没有元素。
    Collection coll = new ArrayList();System.out.println(coll.isEmpty()); // true

4. 清空集合

  • clear():移除集合中所有元素。
    Collection coll = new ArrayList();coll.add(111);System.out.println(coll.isEmpty()); // falsecoll.clear();System.out.println(coll.isEmpty()); // true

5. 判断元素存在性

  • contains(Object obj):通过元素的equals方法判断是否存在指定的对象。

    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));System.out.println(coll.contains(123)); // true
  • containsAll(Collection c):判断当前集合是否包含另一个集合的所有元素。

    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(456);coll2.add(false);System.out.println(coll.containsAll(coll2)); // true

6. 删除元素

  • remove(Object obj):移除集合中与obj相等的元素。只会移除第一个匹配的元素。

    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));System.out.println(coll.remove(123)); // true
  • removeAll(Collection c):从当前集合中移除另一个集合中的所有元素。

    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(456);coll2.add(false);System.out.println(coll.removeAll(coll2)); // true

7. 取两个集合的交集

  • retainAll(Collection c):保留当前集合中与另一个集合相交的元素。
    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(456);coll2.add(789);System.out.println(coll.retainAll(coll2)); // true

8. 判断两个集合是否相等

  • equals(Object obj):比较两个集合是否相等。
    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(123);coll2.add(456);coll2.add(new String("hello"));coll2.add(false);coll2.add(new Person("Tom", 24));System.out.println(coll.equals(coll2)); // true

9. 获取集合的哈希值

  • hashCode():返回集合对象的哈希值。
    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));System.out.println(coll.hashCode()); // 964169686

10. 转换为数组

  • toArray():返回一个Object类型的数组。
    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Object[] arr = coll.toArray();

遍历集合元素(Iterator迭代器)

集合提供了Iterator接口,用于实现逐个访问集合中元素的功能。以下是Iterator的基本使用方法:

获取迭代器

  • iterator():返回一个Iterator对象。
    Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Iterator iterator = coll.iterator();

遍历元素

  • hasNext():判断是否还有下一个元素。
  • next():获取下一个元素并移动游标。
    Iterator iterator = coll.iterator();while (iterator.hasNext()) {  System.out.println(iterator.next());}

Iterator的remove方法

  • remove():通过迭代器对象删除集合中的元素。
    Iterator iterator = coll.iterator();while (iterator.hasNext()) {  Object obj = iterator.next();  if ("Tom".equals(obj)) {    iterator.remove();  }}

foreach循环遍历集合元素

在Java中,可以使用foreach循环来简洁地遍历集合元素。这种方式无需显式获取集合的长度或索引,适合处理不常修改的集合。

使用foreach循环

Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));for (Object obj : coll) {  System.out.println(obj);}

注意事项

  • 如果集合元素是数组,需要使用包装类(如Integer)来确保正确解析。
  • 使用foreach循环时,集合必须实现Iterable接口,这是Collection接口已经实现的。

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

你可能感兴趣的文章
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
Net与Flex入门
查看>>
net包之IPConn
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>
NFS共享文件系统搭建
查看>>