Map in Map in Guava

前端 未结 2 1970
醉梦人生
醉梦人生 2021-02-19 03:28

I have some code with Map> objects, which works (it is instantiated as a HashMap of HashMaps), but I wonder whether there is

相关标签:
2条回答
  • 2021-02-19 04:12

    Table seems well suited for your need. But make sure you choose the proper implementation. In particular, if your second keys are all distinct (the columns in the table) the resulting table will be sparse and you should take that into account to manage memory usage.

    So you should avoid the ArrayTable, but can use any of the other implementations. Note that the docs mention that ImmutableTable has optimized implementations for sparser and denser data sets.

    If your Table is constructed at once, you can use an ImmutableTable.Builder and benefit from this optimisation as well as simplify your life if the table is shared among several threads.

    0 讨论(0)
  • 2021-02-19 04:22

    Take a look at Guava's Table interface.

    Its documentation example seems to overlap quite a bit with your use-case:

    Typically, when you are trying to index on more than one key at a time, you will wind up with something like Map<FirstName, Map<LastName, Person>>, which is ugly and awkward to use. Guava provides a new collection type, Table, which supports this use case for any "row" type and "column" type.

    0 讨论(0)
提交回复
热议问题