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

  • SEARCH
  • Home
  • 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 6224823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:44:04+00:00 2026-05-24T08:44:04+00:00

I need to implement equals() method for an Item that may be put in

  • 0

I need to implement equals() method for an Item that may be put in a hashset of it’s Maker.The Item can have fields as below

class Item{
    private String isbn;
    private String name;
    private double price;
...
}

class Maker{
    private String name;
    private Set<Item> items;
    public Maker() {
        super();
        items = new HashSet<Item>();
    }
...
}

If I implement equals by comparing the three fields and write a hashCode() based on those fields,I will get wrong results when

1.add item to hashset
2.modify the price of item
3.try to find if item exists in hashset


@Override
public boolean equals(Object o){
    if(o == this){
        return true;
    }
    if (!(o instanceof Item)){
        return false;
    }
    Item a = (Item)o;
    if(hasSameName(a) && hasSameIsbn(a) && hasSamePrice(a)){
        return true;
    }
    else{
       return false;
    }
}

@Override
public int hashCode(){
    int hash = 41 + this.isbn.hashCode();
    hash = hash*41+ new Double(this.price).hashCode();
    hash = hash*41 + this.name.hashCode();
    return hash;
}

...
Set<Item> items = new HashSet<Item>();
Item item1 = new Item();
item1.setName("crystal bird");
item1.setIsbn("1111");
item1.setPrice(120.5);

items.add(item1);

System.out.println(items.contains(item1));//returns true
item1.setPrice(177.0);
System.out.println(items.contains(item1));//returns false

What is the solution to overcome this?Should I make hashCode() depend only on isbn and assume that it will not change during the life of the item.

Any help appreciated

mark.

  • 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-24T08:44:05+00:00Added an answer on May 24, 2026 at 8:44 am

    The HashSet.add() method (which is basically HashMap.put() method) finds the best place on the self-maintained inner table according to the hashcode().

    If you change the value of the item That is Key in HashMap so it will alter the hashcode() value resulting abnormal result.

    You should only consider ISBN as a Key for object to keep things as simple as possible.

    @Override
    public boolean equals(Object o){
        if( o == null ){
            return false;
        }
        if(o == this){
            return true;
        }
        if (!(o instanceof Item)){
            return false;
        }
        Item a = (Item)o;
        if( hasSameIsbn(a) ){
            return true;
        }
        else{
           return false;
        }
    }
    
    @Override
    public int hashCode(){
        return (41 + this.isbn.hashCode());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic base repository class for LINQ-to-Entities that can do things like
I need to implement the classic Factory Method pattern in ASP.NET to create server
So, I have an interface with a bunch of methods that need to be
I need to implement 3 public methods that all rely on the same initial
Can we have a not override a concrete method ... compile time error when
I have a DataGrid (called TheGrid) that I would like to implement copy and
I have this method inside an Activity which implements an ImageLoader class: public void
I have an application in Zend where I need to implement system which will
I need to override Equals() method for one of my types but it seems
Can anyone please help me. I have developed macro that keeps track of huge

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.