260. Java 集合 - 深入了解 HashSet 的内部结构

260. Java 集合 - 深入了解 HashSet 的内部结构

🧠 为什么要单独讲 HashSet

虽然 HashSet 看起来只是一个用来保存不重复元素的集合类,但它的内部实现其实是基于 HashMap 的。

👇来看一下它的核心结构:

java 复制代码
private transient HashMap<E, Object> map;
private static final Object PRESENT = new Object();

public boolean add(E e) {
    return map.put(e, PRESENT) == null;
}

📌 结论一:

HashSet 中,你添加的对象其实是被作为 HashMap 的 key 存储的。

  • 所以:HashSet 的元素必须有稳定的 hashCode()equals() 行为。
  • 所以:一旦你修改了已放入 HashSet 中对象的"关键属性",你就可能遇到查找失败、数据重复、集合错乱等奇怪问题。

🧪 示例:向 HashSet 中添加可变对象

我们复用之前的 Key 类,它是一个有 setKey() 方法的可变类

java 复制代码
class Key {
    private String key;

    public Key(String key) {
        this.key = key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String toString() {
        return key;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Key)) return false;
        Key other = (Key) o;
        return Objects.equals(this.key, other.key);
    }

    @Override
    public int hashCode() {
        return key.hashCode();
    }
}

🧪 添加元素并修改 key 值:

java 复制代码
Key one = new Key("1");
Key two = new Key("2");

Set<Key> set = new HashSet<>();
set.add(one);
set.add(two);

System.out.println("set = " + set);

// ❌ 不要在加入 Set 后修改元素!
one.setKey("3");

System.out.println("set.contains(one) = " + set.contains(one));
boolean addedAgain = set.add(one);
System.out.println("addedAgain = " + addedAgain);
System.out.println("set = " + set);
💥 输出示例:
java 复制代码
set = [1, 2]
set.contains(one) = false
addedAgain = true
set = [3, 2, 3]

📌 结论二:

修改对象后,该对象的 hashCode 改变,HashSet 无法识别出它之前已经存过,结果就把同一个对象当成了一个新的对象加入进来。

即使你在 Set 中 只是更新 key 的值,它也会当作是不同的元素对待,结果就是一个集合里"出现两个一样的对象"。


🔍 再验证一下:

java 复制代码
List<Key> list = new ArrayList<>(set);
Key key0 = list.get(0);
Key key2 = list.get(2);

System.out.println("key0 = " + key0);
System.out.println("key2 = " + key2);
System.out.println("key0 == key2 ? " + (key0 == key2));
💡 输出结果:
java 复制代码
key0 = 3
key2 = 3
key0 == key2 ? true

这说明:是同一个对象,但 HashSet 却认为它是不同的!


✅ 正确做法:使用不可变对象作为元素

java 复制代码
    final class SafeKey {
        private final String key;

        public SafeKey(String key) {
            this.key = key;
        }

        public String getKey() {
            return key;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof SafeKey)) return false;
            SafeKey other = (SafeKey) o;
            return Objects.equals(this.key, other.key);
        }

        @Override
        public int hashCode() {
            return key.hashCode();
        }

        @Override
        public String toString() {
            return key;
        }
    }

再试试:

java 复制代码
Set<SafeKey> safeSet = new HashSet<>();
safeSet.add(new SafeKey("1"));
safeSet.add(new SafeKey("2"));
System.out.println("safeSet.contains(new SafeKey(\"1\")) = " + safeSet.contains(new SafeKey("1")));  // true

🎯 总结

规则 建议
HashSet 的底层是 HashMap 元素是作为 key 存储的
Key 必须是 hash 值稳定的 ❌ 不要用可变对象
修改对象后再添加 可能造成"集合污染"
添加后再修改 会导致 contains() 判断失败、重复添加
使用不可变对象 ✅ 最佳实践,推荐 StringInteger 等或自定义不可变类

你可以加一句口号强化记忆:

"对象一变,Set 就乱。"

相关推荐
敲敲了个代码16 小时前
从硬编码到 Schema 推断:前端表单开发的工程化转型
前端·javascript·vue.js·学习·面试·职场和发展·前端框架
WanderInk17 小时前
刷新后点赞全变 0?别急着怪 Redis,这八成是 Long 被 JavaScript 偷偷“改号”了(一次线上复盘)
后端
dly_blog18 小时前
Vue 响应式陷阱与解决方案(第19节)
前端·javascript·vue.js
吴佳浩18 小时前
Python入门指南(七) - YOLO检测API进阶实战
人工智能·后端·python
消失的旧时光-194318 小时前
401 自动刷新 Token 的完整架构设计(Dio 实战版)
开发语言·前端·javascript
console.log('npc')18 小时前
Table,vue3在父组件调用子组件columns列的方法展示弹窗文件预览效果
前端·javascript·vue.js
廋到被风吹走18 小时前
【Spring】常用注解分类整理
java·后端·spring
用户479492835691519 小时前
React Hooks 的“天条”:为啥绝对不能写在 if 语句里?
前端·react.js
我命由我1234519 小时前
SVG - SVG 引入(SVG 概述、SVG 基本使用、SVG 使用 CSS、SVG 使用 JavaScript、SVG 实例实操)
开发语言·前端·javascript·css·学习·ecmascript·学习方法