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

本文共 4645 字,大约阅读时间需要 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()); // false
    coll.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/

你可能感兴趣的文章
Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
查看>>
Node-RED中配置周期性执行、指定时间阶段执行、指定时间执行事件
查看>>
Node-RED安装图形化节点dashboard实现订阅mqtt主题并在仪表盘中显示温度
查看>>
Node-RED怎样导出导入流程为json文件
查看>>
Node-RED订阅MQTT主题并调试数据
查看>>
Node-RED通过npm安装的方式对应卸载
查看>>
node-request模块
查看>>
node-static 任意文件读取漏洞复现(CVE-2023-26111)
查看>>
Node.js 8 中的 util.promisify的详解
查看>>
node.js debug在webstrom工具
查看>>
Node.js RESTful API如何使用?
查看>>
node.js url模块
查看>>
Node.js Web 模块的各种用法和常见场景
查看>>
Node.js 之 log4js 完全讲解
查看>>
Node.js 函数是什么样的?
查看>>
Node.js 函数计算如何突破启动瓶颈,优化启动速度
查看>>
Node.js 切近实战(七) 之Excel在线(文件&文件组)
查看>>
node.js 初体验
查看>>
Node.js 历史
查看>>
Node.js 在个推的微服务实践:基于容器的一站式命令行工具链
查看>>