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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:54:51+00:00 2026-06-17T14:54:51+00:00

this is my first question here, but i hope i will provide all the

  • 0

this is my first question here, but i hope i will provide all the needed information.
If NOT, please let me know!

My Problem:
I tried to improve my backtracking-algorithm by adding a HashMap to store the already processed results. For this i created a own class for the key of that HashMap. In there i overwrote the .equals()- and .hashCode()- methods.
But if i try to put this key and it’s value into the map, it is taking much time, so that the algorithm becomes even less efficient then the backtrack-algorithm without the map.
To solve that problem, i changed the HashMap-Key to String and addded a .toString()-method to my key-class. This works very fine and it is quite fast. (Strange thing: .toString().hashCode() produces a lot of negative values, but seems to work)

Now my Question:
Is it always slowing down that much, if you create your own key?
I tried to find a answer to that question on my own and the only thin i found was to change .hashCode() or playing with the parameters of the HashMap-Constructor.
I tried both and i exported the produced HashCodes for my test-environment and i did not find any duplicates, though i know, it isn’t a “good” method for hash-codes!

Here is a copy of my HashKey-Class (names of variables and methods changed):

public class HashKey {
    private final int int0, int1, int2;

    public HashKey(int int0, int int1, int int2) {
        this.int0 = int0;
        this.int1 = int1;
        this.int2 = int2;
    }

    public int getInt0() {
        return this.int0;
    }

    public int getInt1() {
        return this.int1;
    }

    public int getInt2() {
        return this.int2;
    }

    @Override
    public int hashCode() {
        final int prime1 = 107;
        final int prime2 = 227;
        final int prime3 = 499;
        int result = 1;
        result = prime1 * result + this.int2;
        result = prime2 * result + this.int1;
        result = prime3 * result + this.int0;
        return result;
     }

    @Override
    public String toString() {
        return "Int0: " + this.int0 + " Int1: " + int1 + " Int2: " + int2;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof HashKey) {
            boolean eq0, eq1, eq2;
            eq0 = this.int0 == ((HashKey) obj).getInt0();
            eq1 = this.int1 == ((HashKey) obj).getInt1();
            eq2 = this.int2 == ((HashKey) obj).getInt2();
            if (eq0 && eq1 && eq2) {
                return true;
            }
        }
        return false;
    }
}  

And in my main-Class i use this:

HashMap<HashKey, List<Object>> storedResults = new HashMap<HashKey, List<Object>>();  

int x1,x2,x3;  
Object obj;  

// later in a method:

storedResults.put(new HashKey(x1,x2,x3), obj);

If i change the Type of the Key to String and put that String into the Map, it works fine! So the HashKey.hashCode()-method and the rest of the algorithm works fine and is quite fast.

Does anybody know, what i can do to use this HashKey? For this algorithm it is not that important, but i want to know it for future algorithms!

If there are any questions or critics: they are VERY welcome!

Thanks in advance!

Klumbe

  • 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-06-17T14:54:52+00:00Added an answer on June 17, 2026 at 2:54 pm

    Try this:
    Simplyfy your equals(..)-method and do not calculate the hashCode more than once.

    public final class HashKey {
        private final int int0, int1, int2;
        private final int hashCode;
    
        public HashKey(int int0, int int1, int int2) {
            this.int0 = int0;
            this.int1 = int1;
            this.int2 = int2;
            hashCode=107*int0+227*int1+499*int2;
        }
    
        @Override
        public final int hashCode() {
            return hashCode;
        }
    
        @Override
        public final boolean equals( finalObject obj) {
            if (!obj instanceof HashKey)
                retun false;
            HashKey other = (HashKey)obj;
            return int0 == other.int0 && int1 == other.int1 &&  int2 == other.int2;
        }
    }  
    

    Referring to the comment of fge I changed the code.

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

Sidebar

Related Questions

This is my first question here, but I hope I will get a lot
this is my first question here :) I know that I should not check
Since this is my first question here on stackoverflow I hope my question is
First a caveat: I posted this question here on SuperUser, but it is clearly
This is my first question here on stackoverflow, so I hope that I am
This is my first question on the site and I hope it's not too
This is actually my very first question on Stackoverflow. I hope its not the
Usless Background Info Hello, all. This is my first post here, but I often
this is my first question here. I hope dont make any mistake, be good
First of all; I'm aware that this question is strange and I will probably

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.