[英]Need help with java map and javabean
我有一个嵌套的地图:
Map<Integer, Map<Integer, Double>> areaPrices = new HashMap<Integer, Map<Integer, Double>>();
并使用以下代码填充此地图:
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上填充它,以便可以通过其他方法获取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结构。
如果想法不清楚,请在此处迟到时快速发表评论:)
编辑
我是一个白痴。 我认为数据首先是区域,然后是区域,而获取应该首先是区域,然后是区域。 在这种情况下,地图已经具有正确的结构,首先是区域,然后是区域,因此这不是必需的。 默认情况下,get-get是
public MyBean {
public Map<Integer, Double> get(Integer areaCode) {
return data.get(areaCode);
}
}
首先,您需要做的就是
myBean.getZoneValues(areaCode).get(zoneCode);
while循环很myBean.setZoneValues(areaPrices);
,您需要调用myBean.setZoneValues(areaPrices);
在while循环之外
您无法直接控制第二个get()调用,因为您有嵌套的Map,因此需要返回适当的嵌套Map才能执行所需的操作。 这样的吸气剂应该做到这一点:
public Map<Integer, Double> get(Integer areaCode) {
return zoneValues.get(areaCode);
}
因此,当客户端代码调用get(areaCode)
将返回一个映射,然后他们可以调用get(zoneCode)
。
我建议您进行重构,以消除嵌套的Map,因为您无法阻止客户端代码更改返回的Map,因此代码难以阅读,如果要添加更多功能,您将遇到问题-想象您想在将来提供区号的字符串描述。
像Map<Integer, AreaCode>
这样的东西Map<Integer, AreaCode>
其中AreaCode
是一个对象,其中包含您当前作为嵌套Map拥有的对象)可能是一个不错的起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.