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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:56:11+00:00 2026-05-24T00:56:11+00:00

I have code that looks like the following: private void MethodToDo(SpecialObject o) { Map<InfoObj,

  • 0

I have code that looks like the following:

private void MethodToDo(SpecialObject o) {
    Map<InfoObj, Integer> totalNeeds = new HashMap<InfoObj, Integer>();

    for (ListObject obj : o.getListOfObjects()) {
        InfoObj infoObj = new InfoObj(obj.getTitle(), obj.getId());
        Integer need = totalNeeds.get(infoObj);

        if (need == null) {
           need = new Integer(obj.getNeeded());
        } else {
           need = need + obj.getNeeded();
        }
        totalNeeds.put(infoObj, need); 
    }
}

The object is a private inner class (in the same class as that method) that looks like this:

private class InfoObj {
    private String  title;
    private Integer id;

    public InfoObj(String title, Integer id) {
        this.title = title;
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public Integer getId() {
        return id;
    }

    @Override 
    public boolean equals(Object io2) {
        if (this == io2) { return true; }
        if ( !(io2 instanceof InfoObj) ) { return false; }
        InfoObj temp = (InfoObj) io2;
        return this.id.equals(temp.id) && this.title.equals(temp.title);
    }

    @Override
    public int hashCode() {
        final int prime = 7;
        int result = 1;
        result = prime * result
                + ((this.title == null) ? 0 : this.title.hashCode());
        result = prime * result
                + ((this.id == null) ? 0 : this.id.hashCode());
        return result;
    }

However, despite overriding the equals and hashCode methods, the hashMap will still contain repeat keys (as in title and id are equivalent…but still show up in multiple places). I think I’m doing everything correctly, but realize I could be missing something…

Also, I know there are repeat keys because I loop through the keySet and output the results, which results in objects with the same title and id showing up multiple times.

  • 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-24T00:56:12+00:00Added an answer on May 24, 2026 at 12:56 am

    Per your comment, a HashMap cannot contain the same keys per the implementation the same key would be:

    (e.hash == hash && ((k = e.key) == key || key.equals(k)))
    

    And since you are following the contract for equals and hashcode, any object you create here:

    InfoObj infoObj = new InfoObj(obj.getTitle(), obj.getId());
    

    With the same id and title, will be considered the same key, and if the map previously contained a mapping for the key, the old value is replaced.

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

Sidebar

Related Questions

I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have code that looks like the following, which works fine for displaying the
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have code that looks like this: template<class T> class list { public: class
I have code that looks like this: class T {}; class container { const
Conditions: Windows 98 SE WMI not available I have code that looks like this,
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
I have a code that looks like this: <div id=wrapper> <img id=first class=images src=img/nike.jpg

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.