gpt4 book ai didi

java - 我如何改进 Map>> 使用 Guava

转载 作者:行者123 更新时间:2023-11-29 09:49:38 29 4
gpt4 key购买 nike

实体关系的数据结构如下:

Map<Integer, Map<Integer, Map<PAXType, BigDecimal>>>

实体关系:

  • 1 Package (P) 有许多 Variants (V)
  • 1 Variants (V) 有很多 Price数据点
  • Price基于 PAXType (这是一个枚举:成人、 child 、婴儿)

我使用以下方法建模:

Map<Package, Map<Variant, Map<PAXType, BigDecimal>>>

以快速查询价格为目的

  • 包变体

我现在使用它的方式是:当我从数据库中读取数据时,我创建/更新了上面的 map 。获得所有信息后,对于每个变体,我需要转换价格图,来自 Map<PAXType, BigDecimal>Map<OccupancyType, BigDecimal> ,其中 OccupancyType 是另一个枚举。这是我需要为序列化等输出的最终价格格式。

guava 中是否有任何数据结构适合我拥有的丑陋 map 构造并支持我上面建议的操作?

最佳答案

除了 Tomasz 的回答建议封装 Map<PAXType, BigDecimal>在类里面PriceByType (注意,如果 PAXType 是一个枚举,你应该使用 EnumMap)我认为你应该考虑使用 Guava's Table作为 Map<Integer, Map<Integer, PriceByType>> 的替代品.表格用例:

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.

您的索引是包和包变体,都是整数,所以表应该以 Table<Integer, Integer, PriceByType> 结尾.

关于java - 我如何改进 Map<Integer, Map<Integer, Map<PAXType, BigDecimal>>> 使用 Guava ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11239753/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com