Home首页 / Lesson 6: HashMap methods第 6 课:HashMap 方法
Lesson 6 · Use it第 6 课 · 拿来用
HashMap methods, and how they look in real codeHashMap 的常用方法,以及它们在真实代码里长什么样
Six methods cover almost everything: put, get, getOrDefault, computeIfAbsent, merge, remove.六个方法能盖住几乎所有场景:put、get、getOrDefault、computeIfAbsent、merge、remove。
01The six methods六个方法
| Method方法 | What it does做什么 | Where I use it我用在哪 |
|---|---|---|
| put(k,v) | add or replace添加或覆盖 | save a value by key按 key 存一个值 |
| get(k) | read, null if missing读,没有则返回 null | simple lookup简单查询 |
| getOrDefault(k,d) | read, d if missing读,没有就给默认值 | counters, optional config计数、可选配置 |
| computeIfAbsent(k,f) | compute and store only if missing没有才算并存入 | cache, grouping缓存、分组 |
| merge(k,v,f) | combine old and new合并新旧值 | sum, max, join求和、取最大、拼接 |
| remove(k) | delete删除 | clear, cancel清理、取消 |
02Real code: a cache真实代码:缓存
// 没有就查数据库,再放进缓存(最常见的写法)
User user = cache.computeIfAbsent(userId, id -> userMapper.selectById(id));
One line: read the cache, miss → query the database → write back. No null check, no two lookups.一行:读缓存,没命中——查库——写回。不用判空,也不用查两次。
In a real service add an expiry (Caffeine or Redis). computeIfAbsent keeps the map small, but it never forgets.真实服务里还要加过期(Caffeine 或 Redis)。computeIfAbsent 只是让它懒加载,但不会自己忘。
03Real code: counting真实代码:计数
// 统计每个状态有多少单
Map count = new HashMap<>();
for (Order o : orders) {
count.merge(o.getStatus(), 1, Integer::sum);
}
merge is the cleanest counter: old value + 1, or 1 if it is the first time.merge 是最干净的计数写法:老值 +1,第一次出现就给 1。
04Real code: grouping真实代码:分组
// 按用户分组:Map>
Map> byUser = new HashMap<>();
for (Order o : orders) {
byUser.computeIfAbsent(o.getUserId(), k -> new ArrayList<>()).add(o);
}
Or with the stream API: orders.stream().collect(Collectors.groupingBy(Order::getUserId)) — it does the same thing internally.也可以用 stream:orders.stream().collect(Collectors.groupingBy(Order::getUserId)),内部做的就是同一件事。
05Four things that bite四个坑
- get returns null — use
getOrDefaultwhen the value may be missing.get 返回 null——可能没有的时候用getOrDefault。 - Mutable keys — if you change a field used in
hashCode()afterput, the key is lost.可变的 key——put 之后改了参与hashCode()的字段,这个 key 就找不回来了。 - No order — HashMap does not keep order. Use
LinkedHashMaporTreeMap.无序——HashMap 不保证顺序,要顺序用LinkedHashMap或TreeMap。 - Not thread-safe — lesson 7.线程不安全——第 7 课讲。
→New words新词
Click ▶ hear it for real people on YouGlish; 🔊 for the computer voice.点 ▶ hear it 听真人发音(YouGlish);点 🔊 听电脑朗读。
✓Check yourself自测
- Difference between get and getOrDefault?get 和 getOrDefault 有什么区别?
- Write the one-liner cache with computeIfAbsent.用 computeIfAbsent 写出那一行缓存。
- How do you count occurrences cleanly?怎么干净地做计数?
Answer out loud first. Only then open the answers below.先自己出声答,再看下面的答案。