当前位置: 首页 > news >正文

java基础-集合接口(Collection)

Collection 接口概述

Collection 接口是 Java 集合框架中最基本的接口,它定义了所有集合类都应该具备的基本操作。

主要方法

1. 添加元素

boolean add(E e);           // 添加单个元素
boolean addAll(Collection<? extends E> c); // 添加集合中的所有元素

2. 删除元素

boolean remove(Object o);    // 删除单个元素
boolean removeAll(Collection<?> c); // 删除集合中的所有元素
boolean retainAll(Collection<?> c); // 仅保留指定集合中的元素
void clear();               // 清空集合

3. 查询操作

int size();                 // 返回元素个数
boolean isEmpty();          // 判断是否为空
boolean contains(Object o); // 判断是否包含元素
boolean containsAll(Collection<?> c); // 判断是否包含集合所有元素

4. 集合转换

Object[] toArray();         // 转换为数组
<T> T[] toArray(T[] a);     // 转换为指定类型的数组

5. 迭代器

Iterator<E> iterator();     // 返回迭代器

Collection 接口的主要实现类

// List 接口 - 有序集合
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
List<String> vector = new Vector<>();// Set 接口 - 不重复集合
Set<String> hashSet = new HashSet<>();
Set<String> linkedHashSet = new LinkedHashSet<>();
Set<String> treeSet = new TreeSet<>();// Queue 接口 - 队列
Queue<String> priorityQueue = new PriorityQueue<>();
Queue<String> arrayDeque = new ArrayDeque<>();

实际使用示例

基本操作示例

import java.util.*;public class CollectionExample {public static void main(String[] args) {// 创建集合Collection<String> collection = new ArrayList<>();// 添加元素collection.add("Apple");collection.add("Banana");collection.add("Orange");collection.addAll(Arrays.asList("Grape", "Mango"));System.out.println("集合元素: " + collection);System.out.println("集合大小: " + collection.size());System.out.println("是否包含Apple: " + collection.contains("Apple"));// 删除元素collection.remove("Banana");System.out.println("删除Banana后: " + collection);// 遍历集合System.out.println("遍历集合:");for (String fruit : collection) {System.out.println(fruit);}// 使用迭代器System.out.println("使用迭代器:");Iterator<String> iterator = collection.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}}
}

集合运算示例

public class CollectionOperations {public static void main(String[] args) {Collection<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));Collection<Integer> set2 = new HashSet<>(Arrays.asList(4, 5, 6, 7, 8));// 并集Collection<Integer> union = new HashSet<>(set1);union.addAll(set2);System.out.println("并集: " + union);// 交集Collection<Integer> intersection = new HashSet<>(set1);intersection.retainAll(set2);System.out.println("交集: " + intersection);// 差集Collection<Integer> difference = new HashSet<>(set1);difference.removeAll(set2);System.out.println("差集: " + difference);}
}

Java 8 新增的默认方法

Java 8 为 Collection 接口添加了一些有用的默认方法:

public class CollectionJava8Example {public static void main(String[] args) {Collection<String> collection = new ArrayList<>(Arrays.asList("Java", "Python", "C++", "JavaScript", "Go"));// removeIf - 根据条件删除元素collection.removeIf(s -> s.length() > 4);System.out.println("删除长度>4的元素后: " + collection);// stream - 转换为流long count = collection.stream().filter(s -> s.startsWith("J")).count();System.out.println("以J开头的元素个数: " + count);// forEach - 遍历操作System.out.println("forEach遍历:");collection.forEach(System.out::println);// spliterator - 可分割迭代器Spliterator<String> spliterator = collection.spliterator();spliterator.forEachRemaining(System.out::println);}
}

集合的批量操作

public class BulkOperations {public static void main(String[] args) {Collection<String> source = Arrays.asList("A", "B", "C", "D", "E");Collection<String> target = new ArrayList<>();// 批量添加target.addAll(source);System.out.println("批量添加后: " + target);// 批量删除target.removeAll(Arrays.asList("A", "C"));System.out.println("批量删除后: " + target);// 批量保留target.retainAll(Arrays.asList("B", "D", "F"));System.out.println("批量保留后: " + target);// 判断包含关系System.out.println("是否包含所有元素: " + target.containsAll(Arrays.asList("B", "D")));}
}

重要特性

  1. 泛型支持 - 提供类型安全

  2. 自动装箱 - 基本类型自动转换为包装类

  3. Fail-Fast 迭代器 - 并发修改时快速失败

  4. 可序列化 - 支持对象序列化

  5. 可克隆 - 支持对象克隆

使用建议

  1. 根据需求选择合适的集合实现

  2. 使用泛型确保类型安全

  3. 注意集合的线程安全性

  4. 合理使用批量操作提高性能

  5. 利用 Java 8 的流式操作处理集合数据

Collection 接口为所有集合类提供了统一的操作方式,是理解和使用 Java 集合框架的基础。

http://www.dtcms.com/a/604856.html

相关文章:

  • 基于中国深圳无桩共享单车数据的出行目的推断与时空活动模式挖掘
  • 【Rust】通过系统编程语言获取当前系统内存、CPU等运行情况,以及简单实现图片采集并设置系统壁纸
  • 【计算思维】蓝桥杯STEMA 科技素养考试真题及解析 D
  • 智能合同系统,如何为企业合同管理保驾护航?
  • 基于Rust实现爬取 GitHub Trending 热门仓库
  • 深圳市建设局官方网站曼联对利物浦新闻
  • 【Android 组件】实现数据对象的 Parcelable 序列化
  • CrowdDiff: 使用扩散模型进行多假设人群密度估计
  • 同创企业网站源码wordpress自定义简单注册
  • 在 Android ARM64 上运行 x86_64 程序
  • 幽冥大陆(二十)屏幕录像特效增加节目效果——东方仙盟炼气期
  • 类加载机制、生命周期、类加载器层次、JVM的类加载方式
  • 数据智能开发五 技术架构
  • 免费的app软件下载网站个人网站备案 法律说明
  • MFC Check Box控件完全指南:属性设置、样式定制与高级应用
  • 广州 网站 建设支付网站建设费入什么科目
  • 西宁做网站需要多少钱wordpress怎么安装模板
  • 网站标题优化工具外贸公司电话
  • 北京北排建设公司招标网站wordpress登陆过程
  • 怎么免费做个人网站建设银行网站怎么打印明细
  • 在线设计图片网站总结郑州app拉新项目
  • 网站建设春节放假番禺免费核酸检测
  • preec网站电子商务seo实训总结
  • 优化网站浏览量怎么看网站建设方案评标原则
  • 网站设计职业工作室打开网站搜索
  • 网站建设方案书 doc织梦修改网站背景颜色
  • 网上商城建站服务商wordpress 抄袭查询
  • 网站建设设计猫和老鼠wordpress加备案号
  • 做电影网站 需要进那些群wordpress头像不同步
  • flash网站制作教程网站开发和网站运营的区别