java 从 HashSet 中删除元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29667158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:42:18  来源:igfitidea点击:

Remove element from HashSet

javastringhashset

提问by Keval Trivedi

First of all add the element in HashSet and prints the size of HashSet which returns as expected. but i modified one of the object value and again store in to HashSet and remove the object using object name. but still i get the same size as previous. My code is as under :

首先在 HashSet 中添加元素并打印按预期返回的 HashSet 的大小。但我修改了对象值之一并再次存储到 HashSet 并使用对象名称删除对象。但我仍然得到与以前相同的大小。我的代码如下:

public class Test {

    private String s;
    public Test(String s){
        this.s  = s ;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet<Object> hs = new HashSet<Object>();
        Test t1 = new Test("Keval");
        Test t2 = new Test("Keval");

        String s1 = new String("Keval");        

        hs.add(t1);
        hs.add(t2);
        hs.add(s1);     
        System.out.println("Set Size :: " + hs.size());

        s1 = new String("Demo");        
        hs.remove(s1);
        System.out.println("Set Size :: " + hs.size());


    }
}

Output of above code is :

上面代码的输出是:

Set Size :: 3
Set Size :: 3    // Why it prints 3 insted of 2???

回答by Eran

String s1 = new String("Keval");     
....
hs.add(s1); 
....
s1 = new String("Demo");        
hs.remove(s1);

You are adding a Stringwhich is equal to the String"Keval" to your HashSet, but you are trying to remove a Stringequal to the String"Demo" from the Set.

要添加String这等于String“Keval”到你HashSet,但你正在试图删除String等于String从“演示” Set

Your HashSetcontains no Stringequal to "Demo", so the remove()call removes nothing from the Set, and doesn't affect its size.

您的HashSetcontains no Stringequal to "Demo",因此remove()调用不会从 中删除任何内容Set,并且不会影响其大小。

When you remove the s1 = new String("Demo")line, s1still refers to the Stringthat was added to the Set(the one which is equal to "Keval"), so hs.remove(s1)removes that Stringfrom the Set.

当您删除该s1 = new String("Demo")行时,s1仍然指的 String是添加到的Set(等于“Keval”的hs.remove(s1)那个),因此将其StringSet.

回答by Phoenix

If you want the Hashset to identify your object, you will have to override the equals and hashcode method.

如果您希望 Hashset 识别您的对象,则必须覆盖 equals 和 hashcode 方法。

Since you added "Keval" and tried to remove "Demo" there are no changes to set.

由于您添加了“Keval”并尝试删除“Demo”,因此无需进行任何更改。

Remember since you are using HashSet of Objects, be careful while playing with hashcode and equals method that may have unintended consequences. See this questionfor more detail about this.

请记住,由于您正在使用对象的 HashSet,因此在使用可能会产生意外后果的 hashcode 和 equals 方法时要小心。有关此问题的更多详细信息,请参阅此问题

回答by Aakash

Your problem have multiple issues, and I think you should learn a few basics.

您的问题有多个问题,我认为您应该学习一些基础知识。

  1. Whenever you do a new, it creates a new object. s1 = new String("Demo");

  2. Hashset works on object's hashcode()and equals(). So if you are using your own class to be added to Hashset, please override both of these methods. For more learning, please google them.

  3. Now for your problem, when you created a new object by doing s1 = new String("Demo");and then trying to remove that newobject from hashset by hs.remove(s1);, hashset will use methods equals()and hashcode()to identify the object that should be removed. Since this new object is not present in the hashset, nothing will be removed.

  1. 每当您执行新操作时,它都会创建一个新对象。 s1 = new String("Demo");

  2. Hashset 适用于对象的hashcode()equals()。因此,如果您使用自己的类添加到 Hashset,请覆盖这两个方法。如需更多学习,请谷歌他们。

  3. 现在对于您的问题,当您通过执行创建新对象 s1 = new String("Demo");然后尝试从 hashset by 中删除该对象时hs.remove(s1);, hashset 将使用方法equals()hashcode()识别应删除的对象。由于此新对象不存在于哈希集中,因此不会删除任何内容。

Hence the size is un-changed.

因此尺寸不变。

回答by Prasanna Kumar H A

You are initializing the string as new Stringwhich will create new one in the string pool.If you remove before changing the string value it will remove from hash set and will give size as two(Its because when you added hs.remove,s1 value is "Demo" not "keval".

您正在将字符串初始化为新字符串,这将在字符串池中创建新字符串。如果您在更改字符串值之前删除,它将从哈希集中删除并将大小设为 2(这是因为当您添加时hs.remove,s1 值为“Demo “不是“凯瓦尔”。

    String s1 = new String("Keval");  
    hs.add(t1);
    hs.add(t2);
    hs.add(s1);     
    System.out.println("Set Size :: " + hs.size());  //3
    s1 = new String("Demo"); 
    hs.remove(s1);   //equals() method fails to find Demo and none of the element will be removed
    System.out.println("Set Size :: " + hs.size());

回答by Rajesh Kolhapure

HashSet finds bucket/location in hashset by calculating hashcode of key. While adding s1 in hs your key was "Keval" which will generate a hashcode and s1 object will be stored in that bucket. Now you have changed s1 to "Demo". While removing s1, bucket will be searched based on hashcode generated from key "Demo". This bucket is not found in hs and hence its not deleted. Hashcode of "Keval" and "Demo" is not same. Hope this clears your confusion and answers your query.

HashSet 通过计算key的hashcode在hashset中找到bucket/location。在 hs 中添加 s1 时,您的密钥是“Keval”,它将生成一个哈希码,并且 s1 对象将存储在该存储桶中。现在您已将 s1 更改为“演示”。删除 s1 时,将根据键“Demo”生成的哈希码搜索存储桶。在 hs 中找不到这个存储桶,因此它没有被删除。“Keval”和“Demo”的哈希码不一样。希望这能消除您的困惑并回答您的疑问。

回答by Prasad Kharkar

After you do s1 = new String("Demo"), reference s1is simply referring to the new object and not the one on HashSet.

完成后s1 = new String("Demo"),引用s1只是指新对象,而不是 HashSet 上的对象。

So it is not removing anything from set

所以它不会从集合中删除任何东西