简体   繁体   中英

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

I have created a nested HashMap as follows

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. So how shud I proceed? I have tried creating a List in place of String in timeStampvaluesToStatistics. But then I got stuck in the next part for adding values to each "COUNT" or "MAX" or "MIN"?

  2. Now I want to put values to timeStampvaluesStatisticsTovalues based on "COUNT" or "MIN" or "MAX.

How should I proceed.?

To do a multimap (map of X to Collection<Y> ) then you just do:

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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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