簡體   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