在 Java 中,Collection 是集合框架的根接口之一,它定义了一组对象的基本操作。Java 集合框架主要分为两大分支:Collection(存储单个元素)和 Map(存储键值对)。本文将重点介绍 Collection 接口及其子接口和实现类。
Collection 接口的层次结构
Collection (接口)
├── List (接口)
│ ├── ArrayList
│ ├── LinkedList
│ ├── Vector
│ └── Stack
├── Set (接口)
│ ├── HashSet
│ │ └── LinkedHashSet
│ ├── TreeSet
│ └── SortedSet (接口)
└── Queue (接口)
├── LinkedList
├── PriorityQueue
└── Deque (接口)
├── ArrayDeque
└── LinkedList
Collection 接口的常用方法
所有实现 Collection 的类都必须实现以下核心方法:
add(E e) 添加元素到集合中。
remove(Object o) 移除指定元素。
contains(Object o) 判断集合是否包含指定元素。
size() 返回集合中元素的数量。
isEmpty() 判断集合是否为空。
clear() 清空集合中的所有元素。
iterator() 返回一个迭代器,用于遍历集合中的元素。
toArray() 将集合转换为数组。
List 接口
有序可重复:元素按插入顺序排列,允许重复元素。
常见实现类:ArrayList、LinkedList、Vector。
示例代码
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
// 创建 ArrayList
List<String> fruits = new ArrayList<>();
// 添加元素
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // 允许重复元素
// 访问元素
System.out.println("第一个元素: " + fruits.get(0)); // Apple
// 遍历元素
for (String fruit : fruits) {
System.out.println(fruit);
}
// 删除元素
fruits.remove(1); // 移除 Banana
System.out.println("删除后的集合: " + fruits); // [Apple, Apple]
}
}
Set 接口
无序不可重复:元素不保证顺序(除 LinkedHashSet),且不允许重复。
常见实现类:HashSet、LinkedHashSet、TreeSet。
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// 创建 HashSet
Set<String> names = new HashSet<>();
// 添加元素
names.add("Alice");
names.add("Bob");
names.add("Alice"); // 重复元素会被忽略
// 遍历元素(顺序不保证)
for (String name : names) {
System.out.println(name); // 输出可能是 Alice, Bob 或 Bob, Alice
}
// 判断元素是否存在
System.out.println("是否包含 Bob: " + names.contains("Bob")); // true
// 删除元素
names.remove("Alice");
System.out.println("删除后的集合: " + names); // [Bob]
}
}
迭代器(Iterator)
所有 Collection 都支持通过 iterator() 方法获取迭代器,用于遍历元素:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorExample {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("Java");
collection.add("Python");
collection.add("C++");
// 获取迭代器
Iterator<String> iterator = collection.iterator();
// 遍历元素
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
// 迭代过程中删除元素
if (element.equals("Python")) {
iterator.remove();
}
}
System.out.println("删除后的集合: " + collection); // [Java, C++]
}
}
集合工具类:Collections
Collections 是 Java 提供的静态工具类,包含各种集合操作方法:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(1);
numbers.add(2);
// 排序
Collections.sort(numbers);
System.out.println("排序后的列表: " + numbers); // [1, 2, 3]
// 反转
Collections.reverse(numbers);
System.out.println("反转后的列表: " + numbers); // [3, 2, 1]
// 查找最大值和最小值
System.out.println("最大值: " + Collections.max(numbers)); // 3
System.out.println("最小值: " + Collections.min(numbers)); // 1
}
}
Collection vs. Collections
Collection:是集合框架的根接口,定义了集合的基本操作。
Collections:是工具类,提供静态方法操作集合(如排序、查找等)。
常见面试问题
ArrayList 和 LinkedList 的区别?
ArrayList 基于数组,支持随机访问;LinkedList 基于链表,插入删除效率更高。
HashSet 如何保证元素不重复?
通过 hashCode() 和 equals() 方法判断元素是否重复。
Iterator 和 ListIterator 的区别?
Iterator 只能单向遍历,ListIterator 可以双向遍历且支持修改元素。
如何实现线程安全的集合?
使用 Collections.synchronizedList() 或 CopyOnWriteArrayList。
掌握 Java 集合框架是编写高效、健壮代码的基础,不同的集合类适用于不同场景,选择合适的集合类可以显著提升代码性能。