Is a HashMap a proper data structure

前端 未结 2 1369
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 17:07

I store in a HashMap 3 types of object.

HashMap>

[\'Lorry\', [list of lorries]]
[\'Sport\', [list of sport\'s cars]]
<         


        
2条回答
  •  情话喂你
    2021-01-28 17:57

    The basic approach is sound, however since you only want to store each instance once, a Set is a better choice than a List for the map entry value:

    Map> typeCache = new HashMap>();
    

    The contains() method of HashSet is very fast indeed, so finding if your map contains a particular instance in it values is not going to cost much.

    Using two maps would probably be better though - once for each type of lookup, so also use:

    Map idCache = new HashMap();
    

提交回复
热议问题