Flyinsky's Codes
973 字
5 分钟
集合框架
2024-10-18

Java 集合框架详解#

Java 集合框架(Java Collections Framework, JCF)是 Java 标准库的重要组成部分,用于存储和操作数据的对象。它提供了一种方便的方式来处理动态数量的对象,并且为各种集合类型提供了标准接口和实现。Java 集合框架主要分为三类:ListSetMap,它们分别对应不同的使用场景。本文将详细介绍每种集合类型及其常用实现类,并为每个类型提供代码示例。


1. List 接口#

List 是一种有序的集合,允许元素重复,并且可以通过索引访问元素。常见的 List 实现有 ArrayListLinkedList

- ArrayList#

ArrayList 是基于动态数组的数据结构,支持快速随机访问,适用于频繁读操作的场景。

import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("Apple"); arrayList.add("Banana"); arrayList.add("Cherry"); // 通过索引访问元素 System.out.println("第一项: " + arrayList.get(0)); // 遍历列表 for (String fruit : arrayList) { System.out.println(fruit); } } }

输出:

第一项: Apple Apple Banana Cherry
- LinkedList#

LinkedList 基于双向链表实现,适合频繁插入和删除操作的场景。

import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args) { LinkedList<String> linkedList = new LinkedList<>(); linkedList.add("Car"); linkedList.add("Bike"); linkedList.addFirst("Bus"); // 输出链表 for (String vehicle : linkedList) { System.out.println(vehicle); } // 移除并访问第一个元素 System.out.println("移除第一个元素: " + linkedList.removeFirst()); } }

输出:

Bus Car Bike 移除第一个元素: Bus

2. Set 接口#

Set 是一个无序集合,不允许有重复元素。常见的 Set 实现包括 HashSetTreeSet

- HashSet#

HashSet 基于哈希表实现,元素没有特定顺序,且不允许重复。

import java.util.HashSet; public class HashSetExample { public static void main(String[] args) { HashSet<Integer> hashSet = new HashSet<>(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(20); // 尝试添加重复项 // 遍历HashSet for (Integer number : hashSet) { System.out.println(number); } } }

输出:

10 20 30

注意:重复的 20 不会被添加到集合中。

- TreeSet#

TreeSet 基于红黑树实现,元素将按自然顺序排序。

import java.util.TreeSet; public class TreeSetExample { public static void main(String[] args) { TreeSet<String> treeSet = new TreeSet<>(); treeSet.add("Orange"); treeSet.add("Apple"); treeSet.add("Banana"); // 输出TreeSet(按字典顺序排序) for (String fruit : treeSet) { System.out.println(fruit); } } }

输出:

Apple Banana Orange

3. Map 接口#

Map 是一种键值对集合,它允许通过键快速查找值。键不能重复,值可以重复。常见的 Map 实现包括 HashMapTreeMap

- HashMap#

HashMap 基于哈希表实现,键没有特定的顺序。它允许一个 null 键和多个 null 值。

import java.util.HashMap; public class HashMapExample { public static void main(String[] args) { HashMap<String, Integer> hashMap = new HashMap<>(); hashMap.put("John", 30); hashMap.put("Jane", 25); hashMap.put("Tom", 35); // 通过键获取值 System.out.println("John 的年龄: " + hashMap.get("John")); // 遍历HashMap for (String name : hashMap.keySet()) { System.out.println(name + ": " + hashMap.get(name)); } } }

输出:

John 的年龄: 30 John: 30 Jane: 25 Tom: 35
- TreeMap#

TreeMap 基于红黑树实现,键按自然顺序排序。

import java.util.TreeMap; public class TreeMapExample { public static void main(String[] args) { TreeMap<String, Integer> treeMap = new TreeMap<>(); treeMap.put("Orange", 3); treeMap.put("Apple", 2); treeMap.put("Banana", 4); // 按照键的自然顺序输出 for (String fruit : treeMap.keySet()) { System.out.println(fruit + ": " + treeMap.get(fruit)); } } }

输出:

Apple: 2 Banana: 4 Orange: 3

4. Queue 接口#

Queue 是一种先进先出的集合,常用于任务调度。常见实现包括 LinkedListPriorityQueue

- PriorityQueue#

PriorityQueue 是基于优先级堆的队列,元素按优先级顺序排列。

import java.util.PriorityQueue; public class PriorityQueueExample { public static void main(String[] args) { PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); priorityQueue.add(30); priorityQueue.add(10); priorityQueue.add(20); // 逐个移除并输出元素(按优先级顺序) while (!priorityQueue.isEmpty()) { System.out.println(priorityQueue.poll()); } } }

输出:

10 20 30

总结#

Java 集合框架提供了丰富的数据结构和工具类来处理各种数据类型的集合。在不同的场景下可以根据需求选择合适的集合类型:

  • 频繁访问时使用 ArrayList
  • 频繁插入/删除时使用 LinkedList
  • 需要去重时选择 HashSetTreeSet
  • 需要键值对时使用 HashMapTreeMap

通过对集合类的灵活运用,开发者可以更高效地组织和处理数据。