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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:31:00+00:00 2026-06-01T20:31:00+00:00

I have to develop something like a game of life. For that, I have

  • 0

I have to develop something like a game of life. For that, I have a class called CellPosition that has x and y fields. In order to efficiently use memory, I would like to use some kind of factory method.

CellPosition.at(int x, int y) which would return an instance of CellPosition. I would like though to cache the objects that have the same x, y pair. I though of a List or a HashMap, but I cannot figure out what to use as a key. A concatenation of x and y in a string is doubtingly a good idea.

On the other hand, is it a good idea to just create an object each time and just redefine the equals() method to compare the objects and throw away any caching?

  • 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-01T20:31:01+00:00Added an answer on June 1, 2026 at 8:31 pm

    If you don’t mind using Guava, just:

    1. Make CellPosition instances immutable, then
    2. Use an Interner<CellPosition> (obtained from Interners), then
    3. Move on to solving real problems.

    Something like this:

    class CellPosition
    {
        private static final Interner<CellPosition> CACHE = Interners.newStrongInterner();
        // or .newWeakInterner(), to allow instances to be garbage collected
    
        private final int x;
        private final int y;
    
        private CellPosition(int x, int y)
        {
            this.x = x;
            this.y = x;
        }
    
        public int x() { return x; }
        public int y() { return y; }
    
        public static CellPosition at(int x, int y)
        {
            return CACHE.intern(new CellPosition(x, y));
        }
    
        @Override
        public boolean equals(Object other) {/* TODO */}
    
        @Override
        public int hashCode() {/* TODO */}
    }   
    

    You could also use a Guava Cache instead of an Interner, but there’s not much point since you’d have to construct an int-pair key for the cache — which you’re doing anyway for the interner, in fewer LoC.

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

Sidebar

Related Questions

I have just started to develop a 2D Android game, something like squash a
lets say i have two branches master develop in master i have something like
I have been wondering how tiny url works. I would like to develop something
I have to develop multisite application with single sign on across domains (something like
I use MVC2/asp.net and try to develop something like a wizard. This wizard will
I am willing to develop a mobile application. I wish to have something working
I have a number of modules that I would like to store in my
I'd like to develop a small game but as I created a Windows Phone
I would like to develop a game for windows 8, but since Xna isn't
Trying to develop using MVVM: I have this Csla.PropertyStatus control that is created in

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.