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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:52:12+00:00 2026-05-16T17:52:12+00:00

I have a class representing a set of values that will be used as

  • 0

I have a class representing a set of values that will be used as a key in maps.

This class is immutable and I want to make it a singleton for every distinct set of values, using the static factory pattern.
The goal is to prevent identical objects from being created many (100+) times and to optimize the equals method.

I am looking for the best way to cache and reuse previous instances of this class.
The first thing that pops to mind is a simple hashmap, but are there alternatives?

  • 1 1 Answer
  • 4 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-16T17:52:13+00:00Added an answer on May 16, 2026 at 5:52 pm

    There are two situations:

    • If the number of distinct objects is small and fixed, you should use an enum
      • They’re not instantiatiable beyond the declared constants, and EnumMap is optimized for it
    • Otherwise, you can cache immutable instances as you planned:
      • If the values are indexable by numbers in a contiguous range, then an array can be used
        • This is how e.g. Integer cache instances in a given range for valueOf
      • Otherwise you can use some sort of Map

    Depending on the usage pattern, you may choose to only cache, say, the last N instances, instead of all instances created so far. This is the approach used in e.g. re.compile in Python’s regular expression module. If N is small enough (e.g. 5), then a simple array with a linear search may also work just fine.

    For Map based solution, perhaps a useful implementation is java.util.LinkedHashMap, which allows you to enforce LRU-like policies if you @Override the removeEldestEntry.

    There is also LRUMap from Apache Commons Collections that implement this policy more directly.

    See also

    • Java Tutorials/enums
    • Effective Java 2nd Edition, Item 1: Consider static factory methods instead of constructors
    • Wikipedia/Flyweight pattern

    Related questions

    • Easy, simple to use LRU cache in java
    • LRU LinkedHashMap that limits size based on available memory
      • Guava MapMaker, soft keys and values, etc
    • How would you implement an LRU cache in Java 6?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a class representing access to one table in my database in
I have class method that returns a list of employees that I can iterate
I have class with a member function that takes a default argument. struct Class
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
If I have class ObjA { public ObjB B; } class ObjB { public
Say I have class A with class A { final String foo() { //

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.