Java集合体系结构面试题
基本概念
Q1: Java集合框架的整体架构是怎样的?
Java集合框架主要包含两个大类:
-
Collection接口体系
- List:有序集合
- Set:不重复集合
- Queue:队列集合
-
Map接口体系
- 键值对映射集合
java
public class CollectionHierarchyExample {
public void collectionTypes() {
// List示例
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("a"); // 允许重复
// Set示例
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("a"); // 不允许重复,第二个"a"不会被添加
// Queue示例
Queue<String> queue = new LinkedList<>();
queue.offer("a");
queue.offer("b");
queue.poll(); // 按照FIFO原则获取元素
// Map示例
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
}
}
Q2: 集合框架中的主要接口有哪些?它们有什么特点?
java
public class CollectionInterfacesExample {
public void interfaceFeatures() {
// 1. Collection接口
Collection<String> collection = new ArrayList<>();
collection.add("a");
collection.remove("a");
collection.contains("a");
// 2. List接口
List<String> list = new ArrayList<>();
list.add(0, "a"); // 支持按索引操作
list.get(0);
list.set(0, "b");
// 3. Set接口
Set<String> set = new HashSet<>();
set.add("a"); // 不允许重复元素
set.add("a"); // 返回false
// 4. Queue接口
Queue<String> queue = new LinkedList<>();
queue.offer("a"); // 添加元素
queue.peek(); // 查看头部元素
queue.poll(); // 移除并返回头部元素
// 5. Map接口
Map<String, Integer> map = new HashMap<>();
map.put("a", 1); // 键值对操作
map.get("a");
map.containsKey("a");
map.containsValue(1);
}
}
集合特性
Q3: 集合框架中的顶层接口定义了哪些通用操作?
java
public class CommonOperationsExample {
public void demonstrateOperations() {
Collection<String> collection = new ArrayList<>();
// 1. 添加操作
collection.add("a"); // 添加单个元素
collection.addAll(Arrays.asList("b", "c")); // 添加多个元素
// 2. 删除操作
collection.remove("a"); // 删除单个元素
collection.removeAll(Arrays.asList("b", "c")); // 删除多个元素
collection.clear(); // 清空集合
// 3. 查询操作
collection.contains("a"); // 检查是否包含元素
collection.containsAll(Arrays.asList("a", "b")); // 检查是否包含所有元素
collection.isEmpty(); // 检查是否为空
collection.size(); // 获取大小
// 4. 迭代操作
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
// 处理元素
}
}
}
Q4: 如何正确使用迭代器?
java
public class IteratorExample {
// 1. 基本迭代
public void basicIteration() {
List<String> list = new ArrayList<>();
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
}
// 2. 安全删除元素
public void safeRemoval() {
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
// 错误方式:直接使用集合的remove方法
for (String element : list) {
if ("a".equals(element)) {
list.remove(element); // ConcurrentModificationException
}
}
// 正确方式:使用迭代器的remove方法
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
if ("a".equals(element)) {
iterator.remove();
}
}
}
// 3. 并发修改问题
public void concurrentModification() {
List<String> list = new ArrayList<>();
list.add("a");
// 错误示例
try {
for (String element : list) {
list.add("b"); // ConcurrentModificationException
}
} catch (ConcurrentModificationException e) {
System.out.println("并发修改异常");
}
// 正确示例:使用迭代器的方式修改
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
listIterator.add("b"); // 使用ListIterator的add方法
}
}
}
集合工具类
Q5: Collections工具类提供了哪些常用操作?
java
public class CollectionsUtilExample {
public void demonstrateUtils() {
List<String> list = new ArrayList<>();
// 1. 排序操作
Collections.sort(list); // 自然排序
Collections.sort(list, String::compareTo); // 自定义排序
Collections.reverse(list); // 反转
Collections.shuffle(list); // 随机排序
// 2. 查找和替换
Collections.binarySearch(list, "a"); // 二分查找
Collections.fill(list, "a"); // 填充
Collections.replaceAll(list, "a", "b"); // 替换所有
// 3. 同步包装
List<String> syncList = Collections.synchronizedList(list);
Set<String> syncSet = Collections.synchronizedSet(new HashSet<>());
Map<String, String> syncMap = Collections.synchronizedMap(new HashMap<>());
// 4. 不可修改包装
List<String> unmodifiableList = Collections.unmodifiableList(list);
Set<String> unmodifiableSet = Collections.unmodifiableSet(new HashSet<>());
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(new HashMap<>());
}
}
Q6: 如何选择合适的集合类?
java
public class CollectionSelectionExample {
public void selectionGuidelines() {
// 1. 需要高效随机访问
List<String> arrayList = new ArrayList<>(); // 选择ArrayList
// 2. 需要频繁插入删除
List<String> linkedList = new LinkedList<>(); // 选择LinkedList
// 3. 需要去重
Set<String> hashSet = new HashSet<>(); // 选择HashSet
// 4. 需要排序
Set<String> treeSet = new TreeSet<>(); // 选择TreeSet
// 5. 需要键值对
Map<String, String> hashMap = new HashMap<>(); // 选择HashMap
// 6. 需要线程安全
Map<String, String> concurrentMap = new ConcurrentHashMap<>(); // 选择ConcurrentHashMap
// 7. 需要保证插入顺序
Map<String, String> linkedHashMap = new LinkedHashMap<>(); // 选择LinkedHashMap
// 8. 需要按键排序
Map<String, String> treeMap = new TreeMap<>(); // 选择TreeMap
}
}
面试关键点
- 理解集合框架的整体架构
- 掌握各种集合接口的特点
- 熟悉集合的基本操作
- 正确使用迭代器
- 了解Collections工具类
- 掌握集合类的选择原则
- 注意线程安全问题
- 理解集合的性能特点