在java列表中选择orgaininsng数据的数据结构 [英] Choosing the data structure for orgaininsng data with list of values in java
问题描述
我有一个地图,如下所示,其中有一个键,值为列表
:
Map< String,List< String> newdatamap = new HashMap<>();
map.put(RtyName,Arrays.asList(wpn,wpfnb,dgeft,xbthy));
map.put(rtyRate,Arrays.asList(dd,ww,trrty,httyure))
我想在上一张地图上添加另一张地图,因此有一个键,其值将是上述地图。这是否是正确的数据结构,我们如何实现?
我想要的东西如下所示
键值
B1 RtyName ----> weepn,weepfnb,eedgeft,xbteehy
rtyRate ----->deed,ww,terrty,hteetyure
B2 RtyName ----> SSSweepn,wpefSSSnb,GGeGdgeft,xbteYYYyhy
rtyRate ----->WWded,wTeTYw,YYYYtrerty,IIIehttyure
如上图所示,只有一个新的键被引入到地图,其值是以前的地图。
所以它就像一个 Map< String,Map< String,List< String>>
成为一个复杂的数据结构,我可以将数据结构组织为
,例如一个包含Map的类,因此它将像
映射< B1,RtyName>
地图< B2,rtyRate>
,付款人包含值列表,如
RtyName ----> SSSweepn,wpefSSSnb,GGeGdgeft,xbteYYYyhy
rtyRate ----->deed,ww,terrty,hteetyure
$ c所以在上面的结构中,复杂度将会很低,因为在B1结束时,我必须搜索到的是RtyName的关键字,并且反对付款人的名字。我必须搜索将是wpn,wpfnb,dgeft,xbthy
的值,请告知whheteher地图将是最好的或有其他的更好的数据结构也可以获得这个。
我脑海中的一个数据结构是Guava's Table
final表< String,String,List&String>> values = HashBasedTable.create();
values.put(B1,RtyName,Lists.newArrayList(weepn,weepfnb,eedgeft,xbteehy));
System.out.println(values.get(B1,RtyName)); //打印列表
我的目标是我可以使用的任何数据结构获取Rtyname和Rtyname我将获得可能的值列表
解决方案我会这样做:
映射<整数,列表< String>> dataMap = new HashMap<>();
dataMap.put(B1.hashCode()+RtyName.hashCode(),Arrays.asList(weepn,weepfnb,eedgeft,xbteehy));
dataMap.put(B1.hashCode()+rtyRate.hashCode(),Arrays.asList(deed,ww,terrty,hteetyure));
dataMap.put(B2.hashCode()+RtyName.hashCode(),Arrays.asList(SSSweepn,wpefSSSnb,GGeGdgeft,xbteYYYyhy));
dataMap.put(B2.hashCode()+rtyRate.hashCode(),Arrays.asList(WWded,wTeTYw,YYYYtrerty,IIIehttyure))
哪个代表:
B1,RtyName ----> weepn,weepfnb,eedgeft,xbteehy
B1,rtyRate ----->deed,ww,terrty,hteetyure
B2,RtyName ----> SSSweepn,wpefSSSnb,GGeGdgeft,xbteYYYyhy
B2,rtyRate ----->WWded,wTeTYw,YYYYtrerty,IIIehttyure
请注意, hashCode
只是一个来自 String
满足我需要的类。您可以自己滚动,如果您愿意,则返回一个 String
键(或其他任何东西)。
其实从您的原始方法不需要独立于订单的功能,您甚至可以连接 String
键作为新密钥使用:
dataMap.put(B1+RtyName,Arrays.asList(/ *你的列表* /));
这比第一种方法稍微方便(而不是以编程方式),但是比嵌套 Map
类要好得多。 (和输出的密钥比 hashCode
更容易识别。)
双向映射
作为密钥的值
如果您希望每个列表
关键以及其他方式,您需要第二个地图
:
Map< List< String>,String> valueMap = new HashMap<>(); // new map for value-> key
for(String key:dataMap.keySet())//获取所有键
valueMap.put(dataMap.get(key),key); //创建映射值 - > key
Value作为键的每个项目
如果您希望中的每个 String
项目映射到 keys
以及其他方式,您需要第二个地图
:
映射< String,String> itemMap = new HashMap<>(); // new map for item-> key mapping
for(String key:dataMap.keySet())//获取所有的键并遍历
for(String item:dataMap.get(key)) //对于值列表中的每个项目
itemMap.put(item,key); //创建新的映射项 - >键
I have a map as shown below in which there is a key and values is of type List
:
Map<String, List<String> newdatamap = new HashMap<>();
map.put ("RtyName", Arrays.asList("wpn", "wpfnb", "dgeft", "xbthy"));
map.put ("rtyRate", 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 RtyName ----> "weepn", "weepfnb", "eedgeft", "xbteehy"
rtyRate ----->"deed", "ww", "terrty", "hteetyure"
B2 RtyName ----> "SSSweepn", "wpefSSSnb", "GGeGdgeft", "xbteYYYYhy"
rtyRate ----->"WWded", "wTeTYw", "YYYYtrerty", "IIIehttyure"
As shown above, only a new key is been introduced to the map, and its value is the previous map.
so it is like a Map<String, Map<String, List<String>>>
whis becomes a complex data structure can i organise the data structure as
for example one class containing a Map, so it will be like
Map<B1 , RtyName>
Map<B2 ,rtyRate>
and payer nae contain list of values such as
RtyName ----> "SSSweepn", "wpefSSSnb", "GGeGdgeft", "xbteYYYYhy"
rtyRate ----->"deed", "ww", "terrty", "hteetyure"
so in the above structure complexity will be low since at the end for B1 i have to search key that wiil be RtyName and against payer name further i have to search values which will be "wpn", "wpfnb", "dgeft", "xbthy"
please advise whheteher map will be best or is there any other better data structure also to obtain this .
one data structure coming in my mind is of Guava's Table
final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("B1", "RtyName", Lists.newArrayList("weepn", "weepfnb", "eedgeft", "xbteehy"));
System.out.println(values.get("B1", "RtyName")); // prints the list
My objective is that any data structure I can have in which against B1 I will get Rtyname and for Rtyname I will get possible list of values
解决方案 I would do this:
Map<Integer, List<String>> dataMap = new HashMap<>();
dataMap.put("B1".hashCode()+"RtyName".hashCode(), Arrays.asList("weepn", "weepfnb", "eedgeft", "xbteehy"));
dataMap.put("B1".hashCode()+"rtyRate".hashCode(), Arrays.asList("deed", "ww", "terrty", "hteetyure"));
dataMap.put("B2".hashCode()+"RtyName".hashCode(), Arrays.asList("SSSweepn", "wpefSSSnb", "GGeGdgeft", "xbteYYYYhy"));
dataMap.put("B2".hashCode()+"rtyRate".hashCode(), Arrays.asList("WWded", "wTeTYw", "YYYYtrerty", "IIIehttyure"));
Which would represent:
B1, RtyName ----> "weepn", "weepfnb", "eedgeft", "xbteehy"
B1, rtyRate ----->"deed", "ww", "terrty", "hteetyure"
B2, RtyName ----> "SSSweepn", "wpefSSSnb", "GGeGdgeft", "xbteYYYYhy"
B2, rtyRate ----->"WWded", "wTeTYw", "YYYYtrerty", "IIIehttyure"
Note that hashCode
is just a convient function from the String
class that meets my needs. You could roll your own that returns a String
key (or really anything else) if you preferred.
Actually since your original method didn't require an order independent function, you could really even concatenate the String
keys to use as a new key:
dataMap.put("B1"+"RtyName", Arrays.asList(/*your list here*/));
This is a little less convenient (and not as "good" programmatically) than the first method, but still much better than nesting Map
classes. (And makes keys much easier to recognize when outputted than hashCode
.)
Two-way Mapping
Values as Keys
If you want each List
value to map to keys as well as the other way around, you need a second Map
:
Map<List<String>, String> valueMap = new HashMap<>(); //New map for value->key
for(String key: dataMap.keySet()) //Get all keys
valueMap.put(dataMap.get(key), key); //Create mapping value->key
Each Item in Value as a Key
If you want each String
item in the values
list to map to keys
as well as the other way around, you need a second Map
:
Map<String, String> itemMap = new HashMap<>(); //New map for item->key mapping
for(String key: dataMap.keySet()) //Get all keys and iterate through
for(String item: dataMap.get(key)) //For each item in your value list
itemMap.put(item, key); //Create new mapping item->key
这篇关于在java列表中选择orgaininsng数据的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!