简体   繁体   English

如何在Java中从多嵌套Hashmap分配和检索值?

[英]How to assign and retrieve values from multi nested Hashmap in java?

I have created a nested HashMap as follows 我创建了一个嵌套的HashMap如下

Map<String,String> timeStamptoValues = new HashMap<String, String>();
Map<Map<String,String>,String> timeStampvaluesToStatistics = new HashMap<Map<String,String>, String>();
Map<Map<Map<String,String>,String>,Long> timeStampvaluesStatisticsTovalues = new HashMap<Map<Map<String,String>,String>,Long>();
timeStamptoValues .put("xyz","hello");
timeStampvaluesToStatistics.put(timeStamptoValues,"COUNT");
timeStampvaluesToStatistics.put(timeStamptoValues."MIN");
timeStampvaluesToStatistics.put(timeStamptoValues."MAX");

I am facing two problems 我面临两个问题

  1. When I am inserting values to timeStampvaluesToStatistics to same key it will overwrite the values with the latest entry. 当我将timeStampvaluesToStatistics的值插入相同的键时,它将用最新的条目覆盖这些值。 So how shud I proceed? 那么我该如何进行? I have tried creating a List in place of String in timeStampvaluesToStatistics. 我尝试在timeStampvaluesToStatistics中创建一个代替String的列表。 But then I got stuck in the next part for adding values to each "COUNT" or "MAX" or "MIN"? 但是后来我陷入了为每个“ COUNT”或“ MAX”或“ MIN”添加值的下一部分工作?

  2. Now I want to put values to timeStampvaluesStatisticsTovalues based on "COUNT" or "MIN" or "MAX. 现在,我想基于“ COUNT”或“ MIN”或“ MAX”将值放入timeStampvaluesStatisticsTovalues中。

How should I proceed.? 我应该如何进行?

To do a multimap (map of X to Collection<Y> ) then you just do: 要进行多重映射(将X映射到Collection<Y> ),您只需执行以下操作:

Map<X, List<Y>> (or Set<Y> or whatever).

When you come to add an element just query the existing value. 当您添加元素时,只需查询现有值即可。 If the list exists then add the value to it. 如果列表存在,则将值添加到其中。 If the list does not exist then create a new one, add the new value, then put the new list into the map. 如果列表不存在,则创建一个新列表,添加新值,然后将新列表放入地图中。

No sure if I understood the problem... but what if you use as Key a String/Long and as a value the Map? 不确定我是否理解问题...但是如果您将String / Long和Map用作键,该怎么办? This seems very... odd: 这似乎非常...奇怪:

Map<Map<String,String>,String> timeStampvaluesToStatistics = new HashMap<Map<String,String>, String>();
Map<Map<Map<String,String>,String>,Long> timeStampvaluesStatisticsTovalues = new HashMap<Map<Map<String,String>,String>,Long>();

A map is like a dictionary, searching a word by its definition seems not correct to me. 地图就像字典,按其定义搜索单词对我来说似乎不正确。 In other words, I think your approach does not feel correct. 换句话说,我认为您的方法不正确。

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

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