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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:04:26+00:00 2026-06-14T21:04:26+00:00

I have an X object whose constructor takes in 4 integers fields. To calculate

  • 0

I have an X object whose constructor takes in 4 integers fields. To calculate it’s hash function, I simple throw them in an array and use Arrays.hashCode.

Currently the constructor is private and I have a static creator method. I’d like to memoize construction so that whenever the creator method is called with 4 integer parameters that have been called before, I can return the same object as last time. [Ideally without having to create another X object to compare with.]

Originally I tried a hashSet but that required me to create a new X to check if my hashSet.contains the equal object… nevermind the fact that I can’t ‘get’ out of a hashSet.

My next idea is to use a HashTable which maps:
the hashCode of the int array of the 4 fields –> object. I’m not sure why, but that doesn’t feel right. It feels like I’m doing too much work, isn’t the point of a hashCode to be a sort of mapping to a bunch of objects which calculate to the same hashCode?

I appreciate your advice.

  • 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-14T21:04:27+00:00Added an answer on June 14, 2026 at 9:04 pm

    Not sure how you plan to use the Hashtable but I think below would do your job:

        private static Hashtable<Integer, MyObject> objectInstances = 
                 new Hashtable<Integer, MyObject>();
    
    
        public static MyObject instance(int i1, int i2, int i3, int i4){
             int hashKey = Arrays.hashCode(new int[]{i1, i2,i3,i4});
             //get the object from hashtable
             MyObject myObject = objectInstances.get(hashKey);
    
             //if object was not already created, create now and put in the hashtable
             if(myObject == null){
               myObject = new MyObject(i1,i2,i3,i4);
               objectInstances.put(hashKey, myObject);
             }
            return myObject;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AccountController whose constructor takes an object derived from my custom IOpenIdAuthentication
I have an object whose value may be one of several array types like
I have an instance of an object (Object1) whose constructor creates instances of other
I have an attribute whose constructor takes two strings. I would like to make
In my code, I have a object whose contents is fully garbage. I guess
Lets say that you have a business object whose current state implies that there
Suppose I have a UserControl whose DataContext is set to an object that has
I have a flash object that takes up the whole browser on my website.
I have a nontrivial service object developed with TDD. It started with a simple
I have a rather basic C++ question: Consider a function that takes some input

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.