Java数据结构建议
我是这个领域的新手,请原谅我愚蠢的错误:)所以我面临的问题是: 在我的网页上,我显示了一个表格。目前,我的问题涉及表的三列Java数据结构建议,java,data-structures,jakarta-ee,Java,Data Structures,Jakarta Ee,我是这个领域的新手,请原谅我愚蠢的错误:)所以我面临的问题是: 在我的网页上,我显示了一个表格。目前,我的问题涉及表的三列 First is : Area Code Second is : Zone Code Third is: Value 这三者之间的关系是: 1区号有6个不同的区号,所有这6个区号都有相应的“值” 我需要一个数据结构器,它可以让我灵活地获取区号的“值”,区号属于特定的区号 我对所有区号都有相同的区号: Zone codes are: 111,
First is : Area Code
Second is : Zone Code
Third is: Value
这三者之间的关系是:
1区号有6个不同的区号,所有这6个区号都有相应的“值”
我需要一个数据结构器,它可以让我灵活地获取区号的“值”,区号属于特定的区号
我对所有区号都有相同的区号:
Zone codes are: 111, 222, 333, 444, 555, 666
在浏览了stackoverflow之后,我想我可以采用以下结构:
Map<Integer, Map<Integer, Double>> retailPrices = new HashMap<Integer, Map<Integer, Double>>();
Map<Integer, Double> codes = new HashMap<Integer, Double>();
请帮我解决这个问题。我是否遵循了正确的方法?如果这三项是相关的,我建议创建一个对您的问题有意义的对象,并将它们封装在一起:
public class Area
{
private int areaId;
private List<Zone> zones;
// other code here
}
public class Zone
{
private int zoneId;
private double value;
// other code here
}
公共类区域
{
私有内部区域ID;
私人名单区;
//这里还有其他代码
}
公共课区
{
私人国际区;
私人双重价值;
//这里还有其他代码
}
这样,您就可以在代码中使用列表。您还可以封装任何与这些相关的规则
Java是一种面向对象的语言。停止从原语的角度思考,开始从对象和封装的角度思考。好吧,你看到的问题是 类型映射中的方法put(Integer,Map)不适用于参数(Integer,Double) 是因为插入到
retailPrices
中的行
(new Integer(oResult.getString("AREA")))
作为键(一个整数
),但是
作为价值
Map
类型中方法put
的签名为
V put(K key, V value)
您假设它返回了映射本身,但它实际上返回了您放入其中的值。因此,pegPlPrices.put(Integer,Double)
的类型是Double
将所有这些结合在一起,这意味着您正试图做到这一点:
retailPrices.put(Integer, Double)
您需要将线路分成两部分:
// do the put on pegPlPrices
pegPlPrices.put(new Integer(oResult.getString("ZONE_CODE")), new Double(oResult.getString("VALUE"))
// now add pegPlPrices into your map
retailPrices.put((new Integer(oResult.getString("AREA"))), pegPlPrices));
这应该能帮你解决问题
然而,我认为你选择的结构不足以满足这一要求。如果每个区域代码都有一个与之不分离的内在值和本机值,那么您可能希望将区域代码生成一个类或枚举,并将该值作为私有成员。然后您可以去掉1级Map
s
编辑以添加区域值的枚举骨架
如果要查看区域值的枚举,请参见以下示例:
public enum Zone
{
private Integer zoneCode;
private Double value;
ZONE1( /* Integer, Double */ ),
ZONE2( /* Integer, Double */ );
public Integer getZone()
{
return zoneCode;
}
public Double getValue()
{
return value;
}
private Zone(Integer z, Double v)
{
zoneCode = z;
value = v;
}
}
正如你所说,你对这一点还不熟悉,我还提出了一个抽象概念。即使您不使用它,它也会向您展示一点OO思维:) 区域代码和区域代码一起向我建议了一个位置:
public class Location {
private Integer zoneCode;
private Integer areaCode;
// getters and setters here
// With an equals and hashCode (always implement BOTH or NONE)
// they behave well in a Map, otherwise you can have the same location
// twice if the same (zone, area) are in more than 1 object
// NB these implementations don't like NULL areaCodes or zoneCodes
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (other instanceof Location) {
Location otherLocation = (Location)other;
return (zoneCode == otherLocation.zoneCode) && (areaCode == otherLocation.areaCode);
}
return false;
}
public int hashCode() {
return zoneCode.hashCode() + areaCode.hashCode();
}
}
然后你可以有一个
Map<Location, Double> localizedPrices = new HashMap<Location,Double>();
Map localizedPrices=newhashmap();
如前所述,查看并忽略:)很接近-记住他说过1个区域有6个不同的区号。因此,您可能希望上面的“zone”是列表或数组类型。嗨,duffymo,谢谢您的回复。唯一的关系就是我上面描述的关系。我对此非常陌生,因此将其封装在一个类中将是一个巨大的挑战:(关系是:1区域={6区域=6值}所以每个区号都有6个区域,这些区域有6个值。我向内部hashmap添加值有什么错?为什么会出现错误?+1:自定义类/值对象/Javabean/DTO/POJO/无论你喜欢叫它什么都可以。然后想方设法把它列成一个列表。我只是相信如果这是需要的关系瑞德,你应该在一个对象中强制执行这样的事情。一个映射不会这样做。把你自己从基本体的沟中提出来。想想对象。嗨,菲尔,谢谢你的回答。你能用你的枚举解决方案解释一下吗?我没有得到你提到的内在值和本机值部分?我的意思是,如果值只是一个引用区域的另一种方式(从描述中可以看出)则不应将其视为与区域分开,而应将其视为区域的属性。枚举解决方案与PDO(由duffymo提供)一样适用于此。抱歉,我忘记了值是双精度的。在这种情况下,它不是分区的别名,但我仍然认为,根据设计约束,您可能需要考虑将它们绑定在一起。嗯……我现在很困惑:(所以地图根本不是一个好的选择?
public class Location {
private Integer zoneCode;
private Integer areaCode;
// getters and setters here
// With an equals and hashCode (always implement BOTH or NONE)
// they behave well in a Map, otherwise you can have the same location
// twice if the same (zone, area) are in more than 1 object
// NB these implementations don't like NULL areaCodes or zoneCodes
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (other instanceof Location) {
Location otherLocation = (Location)other;
return (zoneCode == otherLocation.zoneCode) && (areaCode == otherLocation.areaCode);
}
return false;
}
public int hashCode() {
return zoneCode.hashCode() + areaCode.hashCode();
}
}
Map<Location, Double> localizedPrices = new HashMap<Location,Double>();