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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:11:00+00:00 2026-05-12T06:11:00+00:00

I remember eclipse and idea have this template to automatically create an object’s hashCode

  • 0

I remember eclipse and idea have this template to automatically create an object’s hashCode based on its attributes.

One of the strategies if a number and a string is used is something like this.

  return stringValue.hashCode() + intValue * 32;

Ooor something like that.

I don’t have nor eclipse or idea at hand and I would like to create such function.

EDIT

Based on the answers I create this mini-class

    class StringInt {
        private final String s;
        private final int i;

        static StringInt valueOf( String string , int value ) {
            return new StringInt( string, value );
        }
        private StringInt( String string, int value ) {
            this.s = string;
            this.i = value;
        }
        public boolean equals( Object o ) {
            if( o != null && o instanceof StringInt ){
                StringInt other = ( StringInt ) o;
                return this.s == other.s && this.i == other.i;
            }

            return false;
        }
        public int hashCode() {
            return s != null ? s.hashCode() * 37 + i : i;
        }
    }

This class is to be used as key for a large memory map ( > 10k elements ) I don’t want to iterate them each time to find if the String and the int are the same.

Thank you.

ps.. mmh probably it should be names StringIntKey.

  • 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-12T06:11:00+00:00Added an answer on May 12, 2026 at 6:11 am

    Use the Apache Commons HashcodeBuilder:

    public int hashCode() {
        new HashCodeBuilder(17, 37).
               append(myString).
               append(myInt);
    }
    

    Link here:
    http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/HashCodeBuilder.html

    And here:

    http://www.koders.com/java/fidCE4E86F23847AE93909CE105394B668DDB0F491A.aspx

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

Sidebar

Related Questions

I am using Eclipse v3.5. In previous Eclipse versions i remember if i have
I may have a corruption problem in Eclipse run configurations. This happened after I
Recently Eclipse started showing this error in its Error Log: Error validating C:\android-sdk-windows\tools\lib\devices.xml I
As being an IntelliJ IDEA user for 3 years and using eclipse past one
As far as I remember there exists an Eclipse plugin that reveals ids of
I remember hearing somewhere that large functions might have higher execution times because of
Here's a use case: I have a desktop application (built using Eclipse RCP) which
i remember there being a way of marking a section of code in eclipse
I have an existing Dynamic Web Project in Eclipse Indigo and the m2e plugin
Very often while working in Eclipse I realize that I remember class name, but

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.