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 6767819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:59:48+00:00 2026-05-26T14:59:48+00:00

How is the following possible: void contains(LinkedHashSet data, Object arg) { System.out.println(data.getClass()); // java.util.LinkedHashSet

  • 0

How is the following possible:

void contains(LinkedHashSet data, Object arg) {
    System.out.println(data.getClass()); // java.util.LinkedHashSet
    System.out.println(arg.hashCode() == data.iterator().next().hashCode()); // true
    System.out.println(arg.equals(data.iterator().next())); // true
    System.out.println(new ArrayList(data).contains(arg)); // true
    System.out.println(new HashSet(data).contains(arg)); // true
    System.out.println(new LinkedHashSet(data).contains(arg)); // true (!)
    System.out.println(data.contains(arg)); // false
}

Am I doing something wrong?

Obviously, it doesn’t always happen (if you create a trivial set of Objects, you won’t reproduce it). But it does always happen in my case with more complicated class of arg.

EDIT: The main reason why I don’t define arg here is that’s it’s fairly big class, with Eclipse-generated hashCode that spans 20 lines and equals twice as long. And I don’t think it’s relevant – as long as they’re equal for the two objects.

  • 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-26T14:59:48+00:00Added an answer on May 26, 2026 at 2:59 pm

    When you build your own objects, and plan to use them in a collection you should always override the following methods:

    boolean equals(Object o);
    int hashCode();
    

    The default implementation of equals checks whether the objects point to the same object, while you’d probably want to redefine it to check the contents.

    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. To respect the rules an hashCode of an object equals to another one should be the same, thus you’ve also to redefine hashCode.

    EDIT: I was expecting a faulty hashCode or equals implementation, but since your answer, you revealed that you’re mutating the keys after they are added to an HashSet or HashMap.

    When you add an Object to an hash collection, its hashCode is computed and used to map it to a physical location in the Collection.

    If some fields used to compute the hashCode are changed, the hashCode itself will change, so the HashSet implementation will become confused. When it tries to get the Object it will look at another physical location, and won’t find the Object. The Object will still be present if you enumerate the set though.

    For this reason, always make HashMap or HashSet keys Immutable.

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

Sidebar

Related Questions

Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?
I was wondering if the following was possible: class foo { virtual void bar();
I've got a following struct struct teststruct { int *a; void *data; }; Is
Possible Duplicate: Why would one declare a Java interface method as abstract? The following
Let's consider the following code snippet in Java. There are some of possible approaches
Is it possible to somehow implement the following scheme: public interface ISomething { void
Possible Duplicate: Is it safe to delete a void pointer? Will the following code
Is it possible to write the folowing using lambda(C#) private static void GetRecordList(List<CustomerInfo> lstCustinfo)
Is something like the following possible in PHP? $blah = 'foo1'; class foo2 extends
Is the following possible in SQL Server 2000? CREATE FUNCTION getItemType (@code varchar(18)) RETURNS

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.