简体   繁体   中英

What Data Structure to use (in JAVA) with 3 sets of keys in random order

I have 3 sets of keys: one is type, another is subtype and the last one is ID and value is a ProductObj. Originally, I thought of storing the ProductObj in HashMap of HashMap of HashMap ( HashMap<type, HashMap<subtype, HashMap<ID, prodObj>>> ) for faster search (want to design this as a Cache instead of keep retrieving from the DB). [Note: # of productObj is fixed] However, I learned that 50% of the time, I might only get ID and not type/subtype and another 50% would be type/subtype but no ID. What would be a good data structure to suit such purpose?

I thought of HashMap<type, HashMap<subtype, HashMap<ID, uuid>>> and another HashMap<uuid, prodObj> , but I hope to find a even better solution in terms of the data structure. Thanks in advance!

[Additional information]

This is what hope to store {type=1 subtype=1 id=1=prodObj1, type=1 subtype=1 id=2=prodObj2,...}

When id is given: eg id=1, then prodObj1 is returned

When type is given: eg type=1, the both prodObj1 and prodObj2 are returned

When type & subtype are given: eg type=1 subtype=1, the both prodObj1 and prodObj2 are returned

When type, subtype & id are given: eg type=1 subtype=1 id=1, then prodObj1 is returned

I hope to utilize a HashMap like data structure for faster search based on a key value as I will access the cache and change the prodObj state often.

Why using nested maps ?

If you don't manipulate a huge number of values in your map, you could use as key a custom class that defines a composite key (type, subtype and id).

In this way, you could pass to your map an instance of this composite key when you get from or put in.

public class ProductKey{

  private Long id;
  private string type;
  private string subType;

  private ProductKey(){
  }

  public static ProductKey ofWithId(Long id){
    ProductKey productKey = new ProductKey();
    productKey.id = id;  
    return productKey;
  }
   ...
  // other factory methods
   ...
  // equals and hashcode overriden of course
}

And you could instantiate and populate your map in this way :

Map<ProductKey, ProductObj> map = new HashMap<>();
map.put(ProductKey.ofWithId(1), myProductObj);
map.put(ProductKey.ofWithTypeAndSubType("type", "subtype"), anotherProductObj);
 ...

And retrieve element in this way :

 ProductObj retrievedObj = map.get(ProductKey.ofWithId(1));

I would recommend using a single HashMap. Make the key of the map a combination of the three values.

The simplest would be as a String using a delimiter that won't be used in any of the values. For example, using Java 8:

String key = String.join(":", type, subType, id) ;
productMap.put(key, product) ;

The join method is null safe, so it is fine if those values are missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM