Java集合体系结构面试题

Java集合体系结构面试题

基本概念

Q1: Java集合框架的整体架构是怎样的?

Java集合框架主要包含两个大类:

  1. Collection接口体系

    • List:有序集合
    • Set:不重复集合
    • Queue:队列集合
  2. 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
    }
}

面试关键点

  1. 理解集合框架的整体架构
  2. 掌握各种集合接口的特点
  3. 熟悉集合的基本操作
  4. 正确使用迭代器
  5. 了解Collections工具类
  6. 掌握集合类的选择原则
  7. 注意线程安全问题
  8. 理解集合的性能特点
相关推荐
考虑考虑44 分钟前
Jpa使用union all
java·spring boot·后端
用户3721574261351 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊2 小时前
Java学习第22天 - 云原生与容器化
java
渣哥4 小时前
原来 Java 里线程安全集合有这么多种
java
间彧4 小时前
Spring Boot集成Spring Security完整指南
java
间彧4 小时前
Spring Secutiy基本原理及工作流程
java
Java水解5 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆8 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学8 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole8 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端