简体   繁体   English

维护包含地图的地图

[英]Maintaining a map that contains a map

I have a map as shown below in which there is a key and values is of type List :我有一张地图,如下所示,其中有一个键和值是List类型:

Map<String, List<String> newdatamap = new HashMap<>();
map.put ("payerName", Arrays.asList("wpn", "wpfnb", "dgeft", "xbthy"));
map.put ("fixedRate", Arrays.asList("dd", "ww", "trrty", "httyure"))

I'd like to add another map over the previous map, such that there is a key and its value will be the above map.我想在之前的地图上添加另一个地图,这样有一个键,它的值就是上面的地图。 Is this the correct data structure, and how do we implement it?这是正确的数据结构,我们如何实现它?

I want something like this shown below我想要像下面这样的东西

Key         Value

B1          payerName  ----> "wpn", "wpfnb", "dgeft", "xbthy"
            fixedRate ----->"dd", "ww", "trrty", "httyure"



B2          payerName  ----> "SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy"
            fixedRate ----->"WWdd", "wTTYw", "YYYYtrrty", "IIIhttyure"

As shown above, only a new key is been introduced to the map, and its value is the previous map.如上图,只有一个新的key被引入到map中,它的值为之前的map。 Is a Map data structure the correct way to go about this, or do any other data structures work better? Map数据结构是解决此问题的正确方法,还是其他任何数据结构效果更好?

Folks please advise as suggested in answers also will guava library will support JDK 1.5 also人们请按照答案中的建议提出建议,番石榴库也将支持 JDK 1.5

From what it reads like...you want a Map<String, Map<String, List<String>>> .从它读起来就像......你想要一个Map<String, Map<String, List<String>>>

It's easy enough to declare...声明很容易...

Map<String, Map<String, List<String>>> values = new HashMap<>();

...but not enjoyable to index into: ...但不愉快地索引到:

// get B2's fixed rates
values.get("B2").get("fixedRate");

Perhaps instead you should consider Guava's Table class instead.也许您应该考虑使用Guava 的Table类。 It will give you clear coordinates to what object you want to get, as well as preserve the overall structure you have.它将为您提供想要获得的对象的清晰坐标,并保留您拥有的整体结构。

final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("B1", "payerName", Lists.newArrayList("wpn", "wpfnb", "dgeft", "xbthy"));
System.out.println(values.get("B1", "payerName")); // prints the list

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

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