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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:54:59+00:00 2026-05-20T05:54:59+00:00

I need to develop a simple cache (no concurrency or refresh required) to hold

  • 0

I need to develop a simple cache (no concurrency or refresh required) to hold different types of objects. The lookup of these objects may be in a different way. Like lets say we are caching book object which has ISBN number and author. Lookup of this object can be either by ISBN number like

Book lookupBookByISBN(String isbn);

OR it could be a lookupByAuthor like

List lookupBookByAuthor(String authorName);

In a very simple way, it means I can have a Cache object which has two maps one to store book object by ISBN and another to store the same object by authorname.

Like this, think of many such object type like book, so I do not want to store the same object in different maps just because the lookup of them are different.

One way I was thinking of having a single Map whose key is a custom Key object and value is Object (so that I can store any object or list of object)
The Key object is a immutable object which might look like this

public class Key {   
      private final Stirng keyName;   
      private final String keyValue;   
      public Key(String name,String value) {
          this.keyName= name;
          this.keyValue = value;   
      }    
     //getters for keyName and value 
     //hashcode and equals to be put as a key of a map
}

Implementation of lookup method will be

public Book lookupBookByISBN(String isbn) {
    Key key = new Key("ISBN",isbn);
    return ((Book)map.get(key));
}

public List<Book> lookupBookByAuthor(String isbn) {
        Key key = new Key("Author",isbn);
        return (List<Book>map.get(key));
    }

The insert into map needs to be carefully done as the same object needs to be inserted twice into the map.

public void putBook(Book book) {
   Key key = new Key("ISBN",book.getISBN());
   map.put(key,book);
   key = new Key("Author",book.getAuthor());
   List<Book> list = map.get(key);
   if (null == list) {
      list = new ArrayList<Book>();
      map.put(key,book);
   }
   list.add(book);

}

I somehow feel this might not be a good idea and I might need to put the same object in the map N number of times depending upon N dimensions by which I need to lookup the object.

Is there anyother way to design the same in a better way?

  • 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-20T05:55:00+00:00Added an answer on May 20, 2026 at 5:55 am

    When you store an object in a collection (of any kind), you only store a reference to the object. So go ahead and use multiple maps, you will have only one copy of the actual object.

    For example

    Map<String,MyBigObject> map1 = new HashMap...
    Map<String,MyBigObject> map2 = new HashMap...
    MyBigObject mbo = new MyBigObject(...);
    map1.put(mbo.getISBN(),mbo);
    map2.put(mbo.getAuthor(),mbo);
    

    The single object mbo is now accessible via either map.

    EDIT: If you’re worried about the complexity of multiple maps complicating the code, write a class MultiMap that contains all the maps and manages them in whatever way you want. You could have methods add(MyBigObject...) which inserts the object into all the maps using the various property accessors to set the correct key, and then lookup methods such as getByAuthor(...) and getByISBN(...), and whatever else you need. Hide all the complexity behind a simple unified interace.

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

Sidebar

Related Questions

I need to develop a simple form (intended only for printing) to be filled
So, I recently took a job where I need to develop a fairly simple
I need some help! Please. I am trying to develop a very very simple
I need to develop a winform with a treeview menu, localizable and with different
Sometimes I need to develop a simple database application using MS Access Databases. I`ve
I need to develop a lightweight web application, it will: have a simple webgui
I need to develop custom software to do surveys. Questions may be of multiple
I need to develop simple game that will be using peer to peer connection
I need to develop a simple portable C++ program Billing_Unit. It reads some parameters
I develop a simple Google Maps application. I need just to place one marker

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.