繁体   English   中英

如何改善地图 <Integer, Map<Integer, Map<PAXType, BigDecimal> &gt;&gt;使用番石榴

[英]how can i improve upon Map<Integer, Map<Integer, Map<PAXType, BigDecimal>>> using guava

对于实体关系,我具有以下数据结构:

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

实体关系:

  • 1个PackageP )有很多VariantsV
  • 1个VariantsV )具有许多Price数据点
  • Price基于PAXType (这是一个枚举 :成人,儿童,婴儿)

我使用以下方法对此建模:

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

为了基于以下内容快速查找价格

  • 包装变体

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

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

除了Tomasz的答案建议将Map<PAXType, BigDecimal>封装在类PriceByType (请注意,如果PAXType是枚举,则应使用EnumMap),我认为您应该考虑使用Guava的Table代替Map<Integer, Map<Integer, PriceByType>> 表用例:

通常,当您尝试一次在多个键上建立索引时,会遇到诸如Map<FirstName, Map<LastName, Person>> ,使用起来很丑陋且笨拙。 Guava提供了一个新的集合类型Table,它支持任何“行”类型和“列”类型的用例。

您的索引是包和包的变体,都是整数,因此表应该以Table<Integer, Integer, PriceByType>

番石榴在这里无所事事,您需要创建一些名称有意义的对象。 首先将Map<PAXType, BigDecimal>封装到例如持有此地图的PriceByType类中:

Map<Integer, Map<Integer, PriceByType>>

现在以相同的方式处理Map<Integer, PriceByType>

Map<Integer, PriceByVariant>

最终的地图可以称为priceByPackage 现在创建一些帮助程序方法来有效查询这些类:

public class PriceByType {

    private final Map<PAXType, BigDecimal> byType = //...

    public BigDecimal find(PAXType type) {
        return byType.get(type);
    }

}

和:

public class PriceByVariant {

    private final Map<Integer, PriceByType> byVariant = //...

    public BigDecimal find(int variant, PAXType type) {
        //handle unknown values here
        return byVariant.get(variant).find(type);
    }

}

暂无
暂无

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

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