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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:25+00:00 2026-05-23T16:02:25+00:00

What is the best way to implement a cache for Sets? Particularly, what makes

  • 0

What is the best way to implement a cache for Sets? Particularly, what makes the best key for the cache?

In a static factory method, I want to include a caching mechanism, so that I can reuse existing (immutable) objects. This reuse should not come with a significant performance penalty. The critical data of this class is a parametrized LinkedHashSet. I’m wondering if it’s wise to use the hashCode of this Set as key for the cache (HashMap), because in the java documentation it says:
“The hash code of a set is defined to be the sum of the hash codes of the elements in the set”.
Isn’t this potentially a slow process? When is it calculated? As soon as the Set is generated or on demand? Couldn’t this actually eat up lots the performance gains I expect to gain by caching?

Furthermore, hashCode is an int, but HashMaps don’t accept primitives, so this involes boxing to Integer, right?

My current approach, would be to maintain an additional set of the lengthes of sets of the existing objects. The factory method would first check if the current set’s lenght is listed, only then looks up in the actual index. But this also involves boxing…

Is there a better solution?

  • 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-23T16:02:25+00:00Added an answer on May 23, 2026 at 4:02 pm

    You need to use some invariant as the key for each set, something that logically defines the contents of that set.

    Consider creating a NamedSet either wrapping your existing set implementation with a simple Delegator, or subclassing it (if it is not final). Then you can provide an additional key or name field to identify the set and use that as the key for your cache.

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

Sidebar

Related Questions

Which is the best way to implement factory method. 1) A factory method that
What's the best way to implement user controls that require AJAX callbacks? I want
What would be the best way to implement a most-recently-used cache of objects? Here
What is the best way to implement, from a web page a download action
What is the best way to implement mutliple Default Buttons on a ASP.NET Webform?
What's the best way to implement the enum idiom in Ruby? I'm looking for
What is the best way to implement the page view counter like the ones
What's the best way to implement a classic curry function in actionscript with a
Whats the best way to implement rollover 'buttons' like Stackoverflow has for 'Questions', 'Tags',
What's the best way to implement Meebo-style floating windows? I'd prefer to use Prototype

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.