Java集合体系结构面试题
基本概念
Q1: Java集合框架的整体架构是怎样的?
Java集合框架主要包含两个大类:
-
Collection接口体系
- List:有序集合
- Set:不重复集合
- Queue:队列集合
-
Map接口体系
- 键值对映射集合
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: 集合框架中的主要接口有哪些?它们有什么特点?
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: 集合框架中的顶层接口定义了哪些通用操作?
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: 如何正确使用迭代器?
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工具类提供了哪些常用操作?
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: 如何选择合适的集合类?
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工具类
- 掌握集合类的选择原则
- 注意线程安全问题
- 理解集合的性能特点