storing hashMap in a hashMap

2020-02-19 05:58发布

i am reading data from a text file and want to store HashMap in another HashMap..

HashMap<string,HashMap<string,value>>

how to store data and retrieve it? any sample code will be appreciated... thank u

标签: java hashmap
5条回答
Fickle 薄情
2楼-- · 2020-02-19 06:15

HashMap in HashMap will cause problems in readability especially when it goes beyond two levels. I assume that when you read data from a text file you want to categorize the inputs from rows and columns which should be similar to multi-level categories or category within a category. If you can post the sample data and your intention, I could come up with a Custom class example.

public class Category {
  private List<Category> subCategories;
  private List<Item> items;
}

The above data structure will help you solve any level of nesting while categorizing data. This example is specific to a store items' classification.

查看更多
再贱就再见
3楼-- · 2020-02-19 06:27

This will solve the same problem using one map (although, this does not directly answer your question) by flattening two nested maps into one big map, using a double-key.

public class Key2D{
  private final String outer;
  private final String inner;

  public Key2D(String outer, String inner){
    this.outer = outer;
    this.inner = inner;
  }

  //include default implementations for
  //Object.equals(Object) and Object.hashCode()
  //Tip: If you're using Eclipse it can generate
  //them for you.
}

Then just create one map with double-key:

Map<Key2D, Value> map = new HashMap<Key2D, Value>();
map.put(new Key2D("outerKey", "innerKey"), "Value");
map.get(new Key2D("outerKey", "innerKey")); // yields "Value"

This gives a shorter solution. Performance wise it's probably about the same. Memory performance is probably slightly better (just guessing, though).

查看更多
你好瞎i
4楼-- · 2020-02-19 06:38

Creating two Simple Hashmaps: InnerMap and OuterMap

    HashMap<String, HashMap<String, String>> outerMap = new HashMap<String, HashMap<String,String>>();
    HashMap<String, String> innerMap = new HashMap<String, String>();

Populating the HashMaps

    innerMap.put("InnerKey", "InnerValue");
    outerMap.put("OuterKey", innerMap);

Retreiving values from HashMaps

    String value = ((HashMap<String, String>)outerMap.get("OuterKey")).get("InnerKey").toString();
    System.out.println("Retreived value is : " + value);
查看更多
劳资没心,怎么记你
5楼-- · 2020-02-19 06:41

You get something that looks like a 2 dimensions HashMap, so to say. Which means you need 2 String to store a value, and also to retrieve one.

You could, for example write a class to wrap that complexity, like that (untested code):

public class HashMap2D<T> {
    private HashMap<String,HashMap<String,T>> outerMap;

    public HashMap2D() {
        outerMap = new HashMap<String,HashMap<String,T>>();
    }

    public void addElement(String key1, String key2, T value) {
        innerMap=outerMap.get(key1);
        if (innerMap==null) {
            innerMap = new HashMap<String,T>();
            outerMap.put(key1,innerMap);
        }
        innerMap.put(key2,value);
    }

    public T getElement(String key1, String key2) {
        Hashmap innerMap = outerMap.get(key1);
        if (innerMap==null) {
            return null;
        }
        return innerMap.get(key2);
    }
}

If you want methods to process more than one data at a time, it's more complicated, but follows the same principles.

查看更多
Viruses.
6楼-- · 2020-02-19 06:42

Example:

Creating and populating the maps

Map<String, Map<String, Value>> outerMap = new HashMap<String, HashMap<String, Value>>();
Map<String, Value> innerMap = new HashMap<String, Value>();    
innerMap.put("innerKey", new Value());

Storing a map

outerMap.put("key", innerMap);

Retrieving a map and its values

Map<String, Value> map = outerMap.get("key");
Value value = map.get("innerKey");
查看更多
登录 后发表回答