核心内容摘要
爆料!娱乐圈“黑料XX自拍Ⅹ吃瓜”的幕后真相,你绝对想不到!
视频看了几百小时还迷糊关注我几分钟让你秒懂发点评论可以给博主加热度哦
真实痛点为什么你总在“乱用”数据结构用ArrayList存唯一 ID结果重复了用HashMap遍历顺序乱了前端对不上用LinkedList随机访问性能慢成狗高并发下HashMap直接 CPU 100%问题根源你只知道List list new ArrayList()却不知道每种数据结构的底层原理和适用场景本文将带你盘点Java 中最常用的 8 种数据结构结合Spring Boot 真实业务场景 正反案例对比让你从此“精准选型不再踩坑”
Java 核心数据结构速查表数据结构底层实现是否有序是否唯一线程安全典型场景ArrayList动态数组✅ 插入顺序❌❌频繁遍历、按索引访问LinkedList双向链表✅ 插入顺序❌❌频繁头尾插入/删除HashSetHashMapkey❌✅❌去重、快速查找LinkedHashSetLinkedHashMap✅ 插入顺序✅❌去重 保持顺序TreeSet红黑树✅ 自然/自定义排序✅❌排序去重如 TopNHashMap数组链表/红黑树❌key 唯一❌缓存、映射关系LinkedHashMapHashMap 双向链表✅ 插入/访问顺序key 唯一❌LRU 缓存、JSON 保持字段顺序TreeMap红黑树✅ key 排序key 唯一❌范围查询如 18~30 岁用户记住没有“最好”的数据结构只有“最合适”的
手把手实战8 大场景精准选型场景 1用户 ID 去重选 HashSet vs ArrayList❌ 反例用 ArrayList 去重// 错O(n²) 时间复杂度数据量大直接卡死 ListLong userIds new ArrayList(); if (!userIds.contains(userId)) { // contains 是 O(n) userIds.add(userId); }✅ 正确用 HashSetSetLong userIds new HashSet(); userIds.add(userId); // 自动去重O(
平均时间原理HashSet底层是HashMapkey 存元素value 是固定对象。
场景 2返回用户列表要求顺序和数据库一致选 LinkedHashSet❌ 反例用 HashSet顺序乱了SetUser users new HashSet(); users.addAll(dbUsers); // 顺序丢失前端展示错乱✅ 正确用 LinkedHashSetSetUser users new LinkedHashSet(); users.addAll(dbUsers); // 保持插入顺序✅ 适用前端需要按“创建时间”顺序展示且要去重。
场景 3获取年龄最小的 10 个用户选 TreeSet// 按年龄升序自动排序 SetUser top10Youngest new TreeSet(Comparator.comparing(User::getAge)); for (User user : allUsers) { top10Youngest.add(user); if (top10Youngest.size()
{ top10Youngest.remove(top10Youngest.last()); // 移除最大年龄 } }TreeSet底层是红黑树插入即排序适合 TopN、范围筛选。
场景 4缓存用户信息选 HashMap vs TreeMap❌ 反例用 TreeMap 查用户没必要排序MapLong, User cache new TreeMap(); // O(log n) 查询 User user cache.get(1001L); // 比 HashMap 慢✅ 正确用 HashMapMapLong, User cache new HashMap(); // O(
查询除非需要按 key 排序或范围查询否则优先 HashMap场景 5实现 LRU 缓存选 LinkedHashMappublic class LRUCacheK, V extends LinkedHashMapK, V { private final int capacity; public LRUCache(int capacity) { super(capacity,
75f, true); // accessOrdertrue 表示按访问顺序 this.capacity capacity; } Override protected boolean removeEldestEntry(Map.EntryK, V eldest) { return size() capacity; // 超过容量自动移除最老元素 } } // 使用 LRUCacheString, String cache new LRUCache(
; cache.put(user:1001, 张
;✅LinkedHashMap的accessOrdertrue是实现 LRU 的关键场景 6高频并发读写缓存选 ConcurrentHashMap❌ 反例用 HashMap synchronized性能差private final MapString, Object cache new HashMap(); public synchronized Object get(String key) { ... } // 整个 map 锁住✅ 正确用 ConcurrentHashMapprivate final MapString, Object cache new ConcurrentHashMap(); // 无锁读分段写高并发神器 Object value cache.computeIfAbsent(key, k - loadFromDB(k)); Spring 的Cacheable默认就用ConcurrentHashMap做本地缓存场景 7队列任务处理选 ArrayDeque vs LinkedList❌ 反例用 LinkedList 当队列QueueTask queue new LinkedList(); // 内存占用高缓存不友好✅ 正确用 ArrayDequeQueueTask queue new ArrayDeque(); // 数组实现更快更省内存 Java 官方推荐优先用ArrayDeque代替Stack或LinkedList做队列/栈场景 8统计词频选 HashMap mergeMapString, Integer wordCount new HashMap(); words.forEach(word - wordCount.merge(word, 1, Integer::sum) // 不存在则设1存在则1 );✅ 比if-else判空简洁 10 倍
Spring Boot 中的经典应用
Controller 返回 JSON 字段顺序用 LinkedHashMapGetMapping(/user) public MapString, Object getUser() { // 保证 JSON 字段顺序id, name, email MapString, Object result new LinkedHashMap(); result.put(id,
; result.put(name, 张
; result.put(email, zhangsanexample.com); return result; }否则HashMap可能返回{ email: ..., id: 1001, name: ... }前端解析混乱。
配置多策略 Bean用 Map 策略模式Service public class PaymentContext { // 自动注入所有 PaymentStrategykey 为 beanName private final MapString, PaymentStrategy strategyMap; public PaymentContext(MapString, PaymentStrategy strategies) { this.strategyMap new HashMap(strategies); } public void pay(String type, BigDecimal amount) { strategyMap.get(type).pay(amount); // O(
查找 } }这里必须用HashMap因为要快速根据类型找到策略
避坑指南常见误区⚠️ 误区 1“List 就是 ArrayList”错声明时用接口实现时再选ListString list new ArrayList(); // ✅ ArrayListString list new ArrayList(); // ❌ 不利于替换⚠️ 误区 2“Set 都是无序的”错LinkedHashSet和TreeSet都是有序的。
⚠️ 误区 3“HashMap 线程安全”大错特错高并发下HashMap会死循环JDK 7或数据错乱JDK 8并发场景请用ConcurrentHashMap。
六、