更改HashSet中的值 [英] Changing values in HashSet

查看:461
本文介绍了更改HashSet中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了以下问题:更改集合中的元素会更改等于"语义

I've read this question: Changing the elements in a set changes the 'equals' semantics

但是,我不知道该如何解决无法更改HashSet中的项目并稍后将其删除的问题.

However, I don't know how to solve the problem that I can't change an item in the HashSet and remove it later.

我有一些示例源代码:

public static void main(String[] args) {
    TestClass testElement = new TestClass("1");
    Set<TestClass> set = new HashSet<>();
    set.add(testElement);
    printIt(testElement, set, "First Set");
    testElement.setS1("asdf");
    printIt(testElement, set, "Set after changing value");
    set.remove(testElement);
    printIt(testElement, set, "Set after trying to remove value");
    testElement.setS1("1");
    printIt(testElement, set, "Set after changing value back");
    set.remove(testElement);
    printIt(testElement, set, "Set removing value");
}

private static void printIt(TestClass hullo, Set<TestClass> set, String message) {
    System.out.println(message + " (hashCode is " + hullo.hashCode() + "):");
    for (TestClass testClass : set) {
        System.out.println("    " + testClass.toString());
        System.out.println("        HashCode: " + testClass.hashCode());
        System.out.println("        Element is equal: " + hullo.equals(testClass));
    }
}

TestClass只是一个POJO,它拥有一个变量(加上getter和setter),并实现了hashcode()和equals().

Where TestClass is just a POJO that holds a variable (plus getter & setter) and has hashcode() and equals() implemented.

有人要求显示equals()和hashcode()方法.这些是由eclipse自动生成的:

There was a request to show the equals() and hashcode()-methods. These are autogenerated by eclipse:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((s1 == null) ? 0 : s1.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    TestClass other = (TestClass) obj;
    if (s1 == null) {
        if (other.s1 != null)
            return false;
    } else if (!s1.equals(other.s1))
        return false;
    return true;
}

结果如下:

First Set (hashCode is 80):
    TestClass [s1=1]
        HashCode: 80
        Element is equal: true
Set after changing value (hashCode is 3003475):
    TestClass [s1=asdf]
        HashCode: 3003475
        Element is equal: true
Set after trying to remove value (hashCode is 3003475):
    TestClass [s1=asdf]
        HashCode: 3003475
        Element is equal: true
Set after changing value back (hashCode is 80):
    TestClass [s1=1]
        HashCode: 80
        Element is equal: true
Set removing value (hashCode is 80):

更改哈希码后,我无法从HashSet中删除该值.就像在链接的问题中一样,我理解为什么是那样的,但是我不知道知道如何删除更改的值.有可能这样做吗?

When the hashcode has changed, I can't remove the value from the HashSet. As in the linked question, I understand why it is like that, but I don't know how to delete a changed value. Is there any possibility to do so?

推荐答案

您正面临问题,因为哈希集中的键不是不可变的.如果没有不变的键,则一旦修改,您将失去对原始键对象的引用.而且将永远无法得到解决,这有时被称为集合中的内存泄漏.因此,如果您使用不可变键,就不会遇到这种情况.

You are facing the problem because the keys in your hashset are not immutable. If you don't have immutable keys, you will lose the reference of the original key object once modified. And will never be able to get handle of that, which is sometimes referred as memory leaks in collection. So if you use immutable keys, you wouldn't run into this situation.

这篇关于更改HashSet中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆