Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:31:29+00:00 2026-05-30T12:31:29+00:00

HashSet in java confused me a lot, when using contains() will it look up

  • 0

HashSet in java confused me a lot, when using contains() will it look up hashcode() and equals() result ?
But in this case, it doesn’t behave normal.
Sometime it’s problematic if you put this kind of code in large project.
Problem is why last statement print FALSE ?what contains() do under the hood ?

class R
{
    int count;
    public R(int count)
    {
        this.count = count;
    }
    public String toString()
    {
        return "R(count attribution:" + count + ")";
    }
    public boolean equals(Object obj)
    {
        if (obj instanceof R)
        {
            R r = (R)obj;
            if (r.count == this.count)
            {
                return true;
            }
        }
        return false;
    }
    public int hashCode()
    {
        return this.count;
    }
}
public class TestHashSet2
{
    public static void main(String[] args) 
    {
        HashSet hs = new HashSet();
        hs.add(new R(5));
        hs.add(new R(-3));
        hs.add(new R(9));
        hs.add(new R(-2));

        System.out.println(hs);

        //change first element
        Iterator it = hs.iterator();
        R first = (R)it.next();
        first.count = -3;
        System.out.println(hs);
        //remove
        hs.remove(new R(-3));
        System.out.println(hs);

        R r1 = new R(-3);
        System.out.println(r1.hashCode());
        Iterator i = hs.iterator();
        R r2 = (R)i.next();
        System.out.println(r2.hashCode());   //same hashcode -3
        System.out.println(r1.equals(r2));   //equals true

        System.out.println("hs contains object which count=-3 ?" + hs.contains(new R(-3)));  //false
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:31:31+00:00Added an answer on May 30, 2026 at 12:31 pm

    The HashSet stores the values in buckets, the bucket index is calculated when you add the element to the hash set. The idea behind it: now the set can read an objects hashcode and calculate the bucket in a single step. In other words: contains() is a O(1) operation.

    Imagine a trivial hash set:

    bucket    object(hashcode)
    #1        5
    #2        -3
    #3        6
    

    with a hash function to calculate buckets like:

    f(hashcode) :=  |  5 -> 1
                    | -3 -> 2
                    |  6 -> 3
    

    Now have look at what you done in your example: you’ve removed the object in bucket 2 (changes the function) and changed the hashcode of the object in bucket 1.

    The new function looks like:

    f(hashcode) :=  |  5 -> 1
                    |  6 -> 3
    

    f(-3) will return null (contains() returns false) and your actual object with hashcode -3 is stored where an object with hashcode 5 should be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If every object added to a java.util.HashSet implements Object.equals() and Object.hashCode() in a deterministic
How to use HashSet<string>.Contains() method in case -insensitive mode?
Let's say I have this code in Java: HashSet<String> wordSet = new HashSet<String>(); String
I'm using Spring to load a Set containg integers: <util:set id=ModifiableTags set-class=java.util.HashSet> <value>44</value> <value>38</value>
Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object> ,
My xml looks like this: <bean name=subscriberStore class=java.util.HashSet scope=singleton/> And I have the following
So, if I try to remove elements from a Java HashSet while iterating, I
Does anyone know how Java implements its hash tables (HashSet or HashMap)? Given the
I am using a HashSet in my WCF interface [ServiceContract] public interface IwcfServerSync {
I feel like a novice for asking this question -- but why is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.