gpt4 book ai didi

java - 需要有关 java map 和 javabean 的帮助

转载 作者:行者123 更新时间:2023-11-29 06:22:34 24 4
gpt4 key购买 nike

我有一个嵌套 map :

Map<Integer, Map<Integer, Double>> areaPrices = new HashMap<Integer, Map<Integer, Double>>();

此 map 使用以下代码填充:

 while(oResult.next())

{

Integer areaCode = new Integer(oResult.getString("AREA_CODE"));
Map<Integer, Double> zonePrices = areaPrices.get(areaCode);
if(zonePrices==null)
{
zonePrices = new HashMap<Integer, Double>();
areaPrices.put(areaCode, zonePrices);
}
Integer zoneCode = new Integer(oResult.getString("ZONE_CODE"));
Double value = new Double(oResult.getString("ZONE_VALUE"));
zonePrices.put(zoneCode, value);

myBean.setZoneValues(areaPrices);

}

我想在同一个类的另一个方法中使用这个 Map 的值。为此,我有一个 bean 。

我如何在 bean 上填充它,以便我可以在这个其他方法中获得 ZONE_VALUE

在我的 bean 中,我添加了一个新字段:

private Map<Integer, Map<Integer, Double>> zoneValues;

getter 和 setter 为:

public Map<Integer, Map<Integer, Double>> getZoneValues() {
return zoneValues;
}

public void setZoneValues(Map<Integer, Map<Integer, Double>> areaPrices) {
this.zoneValues = areaPrices;
}

我想用另一种方法做的是这样的:

Double value = myBean.get(areaCode).get(zoneCode);

我如何让它发生:(

最佳答案

我想建议一个不同的,希望更具可读性的解决方案:

public class PriceMap {
private Map<Integer, Map<Integer, Double>> priceMap =
new HashMap<Integer, Map<Integer, Double>>();

// You'd use this method in your init
public Double setPrice(Integer areaCode, Integer zoneCode, Double price) {
if (!priceMap.containsKey(zoneCode)) {
priceMap.put(zoneCode, new HashMap<Integer, Double>());
}
Map<Integer, Double> areaMap = priceMap.get(zoneCode);
areaMap.put(areaCode, price);
}

public void getPrice(Integer areaCode, Integer zoneCode) {
if (!priceMap.containsKey(zoneCode)) {
// Eek! Exception or return null?
}
Map<Integer, Double> areaMap = priceMap.get(zoneCode);
return areaMap.get(areaCode);
}
}

我认为这是一个更好、更易读的抽象,非常重要的是,它使您或其他人在几个月后更容易阅读。

编辑 添加获取

如果您遇到 get(areaCode).get(zoneCode)(顺序颠倒)问题,但 myBean 完全属于您,您可以执行以下操作:

public class MyBean {
// I suppose you have this already
private final Map<Integer, Map<Integer, Double>> priceMap =
new HashMap<Integer, Map<Integer, Double>>();

private class LooksLikeAMap implements Map<Integer, Double> {
private Integer areaCode = areaCode;
public LooksLikeAMap(Integer areaCode) {
this.areaCode = areaCode;
}

public Double get(Object zoneCode) {
if (!priceMap.containsKey(zoneCode)) {
// Eek! Exception or return null?
}
Map<Integer, Double> areaMap = priceMap.get(zoneCode);
return areaMap.get(areaCode);
}
// Implement other methods similarly
}

public Map<Integer, Double> get(Integer areaCode) {
return new LooksLikeAMap(areaCode);
}
}

好吧,在 HTML textarea 中编程不是我的强项,但思路很清晰。制作一些由完整数据集支持的类似 Map 的结构,并对其进行初始化具有所需 AreaCode 的 map 结构。

如果想法不清楚,请快速发表评论,因为这里已经晚了:)

编辑

我是个傻子。我认为数据首先是区域,然后是区域,而获取应该是先是区域,然后是区域。在这种情况下, map 已经具有正确的结构,首先是区域,然后是区域,因此这不是必需的。 get-get 是默认的,如果你做

public MyBean {
public Map<Integer, Double> get(Integer areaCode) {
return data.get(areaCode);
}
}

关于java - 需要有关 java map 和 javabean 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2457412/

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