21

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

0

5 Answers 5

40

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");
Sign up to request clarification or add additional context in comments.

5 Comments

Note: in java its String with a upper S.
Edit: Map<String, Map<String, Value>> outerMap = new HashMap<String, Map<String, Value>>(); I was getting a type mismatch exception.
@Johan What if I need to update the inner map after putting it in the outer map? Can that be done?
@Sohaib You would have to get() the innerMap, update it then put it again.
@LukeChamberlain I don't think put() again is required since the get would return a reference. Just get() and updating it would be enough. I did some experiments yesterday and it worked.
6

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

4 Comments

There is no use to store the same instance of innerMap for each key in outerMap, it would be the same as not having outerMap. You should create a new instance of the "inner map" for each new key in outerMap.
@Trinidad: Thats absolutely correct. For each new key in outermap there will be a new instance of innerMap. I was just trying to demonstrate "how to populate the value in outermap with one key, and then just retreiving its value".
Oh, but it confuses to instantiate both maps in the same place, you should have put the creation of the new inner map instance with the code to populate it. =)
So you are nesting Hashmaps, but just to avoid retrieving the inner one, you recreate it each time. That's a waste of performance.
4

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.

Comments

2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.