简体   繁体   English

使用哪种数据结构(在JAVA中)与3组键以随机顺序使用

[英]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. 我有3组键:一个是类型,另一个是子类型,最后一个是ID,值是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). 最初,我想将ProductObj存储在HashMap的HashMap的HashMap中( HashMap<type, HashMap<subtype, HashMap<ID, prodObj>>> ),以加快搜索速度(希望将其设计为Cache而不是从数据库中不断获取) )。 [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. [注意:productObj的数量是固定的]但是,我了解到50%的时间中,我可能只会获得ID而不是类型/子类型,另外50%是类型/子类型,但没有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. 我想到了HashMap<type, HashMap<subtype, HashMap<ID, uuid>>>和另一个HashMap<uuid, prodObj> ,但是我希望在数据结构方面找到更好的解决方案。 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,...} 这就是希望存储的内容{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 当给定id时:例如id = 1,则返回prodObj1

When type is given: eg type=1, the both prodObj1 and prodObj2 are returned 当给定类型时:例如,type = 1,则返回prodObj1和prodObj2

When type & subtype are given: eg type=1 subtype=1, the both prodObj1 and prodObj2 are returned 当给出type和subtype时:例如type = 1 subtype = 1,则同时返回prodObj1和prodObj2

When type, subtype & id are given: eg type=1 subtype=1 id=1, then prodObj1 is returned 当给出类型,子类型和id时:例如,type = 1子类型= 1 id = 1,则返回prodObj1

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. 我希望利用类似HashMap的数据结构来基于键值进行更快的搜索,因为我将访问缓存并经常更改prodObj状态。

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). 如果您不操作地图中的大量值,则可以将定义复合键(类型,子类型和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. 我建议使用单个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. 最简单的将是使用分隔符的String,该分隔符将不会在任何值中使用。 For example, using Java 8: 例如,使用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. join方法是空安全的,因此如果缺少这些值就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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