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

  • Home
  • SEARCH
  • 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 6815785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:50:54+00:00 2026-05-26T20:50:54+00:00

I need a Map to make a cache in Java for same values that

  • 0

I need a Map to make a cache in Java for same values that I have two String keys. My question it’s better to make nested Maps (one for each key) or make some type of custom key maked with the two Strings?

Access to data on cache will all time accessed with the two keys and I don’t need group it by any of that two keys.

Then if is better combine string key in only one what it’s better?

  • Custom class with custom getHash method. But then the problem is what hash function implement?
  • Simply concatenate two strings together. For example:

    cache.put(key1+key2, value)

  • 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-26T20:50:54+00:00Added an answer on May 26, 2026 at 8:50 pm

    You can either make nested maps or use a custom class defining hashCode().

    It’s usually not a good idea to concatenate the keys, you can end up with collisions, as in the case with keys 1 and 22 and keys 12 and 2. They’d map to the same value 122.

    If you’ll always use both keys, using a single Map will always be a little more efficient, and you can always define your own adapter to the map that will take two arguments:

    public class MyCache { 
    
        private Map<MyKey, Object> cache = new HashMap<MyKey, Object>();
    
        public Object getObject(Object key1, Object key2){
            return cache.get(new MyKey(key1, key2));
        }
        public void putObject(Object key1, Object key2, Object value){
            cache.put(new MyKey(key1, key2), value);
        }
    }
    

    Remember to define equals() and hashCode() in your custom key class (add checks for nullity if needed).

    public int hashCode() {
        int result = 17;
        result = 37 * result + keyA.hashCode();
        result = 37 * result + keyB.hashCode();
        return result;
    }
    
    public boolean equals(Object another) { 
        return another.keyA.equals(keyA) && another.keyB.equals(keyB);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make map where Dates are keys. 2 date objects are equals
I'd like to map the following sql in NHibernate. Will I need to make
I have a simple requirement, i need a map of type . however i
I have a number, say 123456, and I need to map it to a
So I have a class that is a generic and it may need to,
I need to map a delete ptrAddr; to a boost::function0 but i have some
I have an array of strings which need to be taken into a map.
For an project I need to as efficiently as possible make a world map
I'm looking looking for a tool that will allow me to make a map
I need to make my google map V3 a full circle. I use CSS3

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.