开发者

storing hashMap in a hashMap

i am reading data from a text file and want to store HashMap in ano开发者_StackOverflow社区ther HashMap..

HashMap<string,HashMap<string,value>>

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


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");


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);


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜