Storing a HashMap inside another HashMap and improving performance

I have to create HashMapinside another HashMap, as shown below, which can store the value inside the internal HashMapone based on the key of the external one HashMapat runtime

i.e. the required output for the program must be in the format

   { 1 = {11 = "aaa",15 = "bbb"}, 2 = {13 = "ccc", 14 = "ddd"} }

where 1,2 is the key value for the Outer HashMap.

Below is the code provided for it. Is there a better approach to increase productivity.

HashMap<Integer, HashMap<Integer, String>>Outer 
                   = new HashMap<Integer, HashMap<Integer,String>>();

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int count = Integer.parseInt(br.readLine());
    for(int i =0;i<count;i++)
    {
        String input[] = br.readLine().split("\\s");

        //HashMap<Integer,String>inner = new HashMap<Integer, String>();
        int key = Integer.parseInt(input[0]);
        if(Outer.isEmpty() || !Outer.containsKey(key))
        {
            HashMap<Integer, String> inner = new HashMap<Integer, String>();
            inner.put(Integer.parseInt(input[1]),input[2]);
            Outer.put(key, inner);
        }
        else if(Outer.containsKey(key))
            {
                HashMap<Integer, String> inner = (HashMap<Integer, String>) Outer.get(key).clone();
                inner.put(Integer.parseInt(input[1]), input[2]);
                Outer.put(key, inner);
            }
    }
+5
source share
3 answers

Like Vadim, but even more improved - because it does not require a call both containsKey, and get:

Map<Integer, Map<Integer, String>> outer = new HashMap<Integer, Map<Integer, String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());

Pattern splitter = Pattern.compile("\\s");

for(int i = 0; i < count; i++){
    String input[] = splitter.split(br.readLine());

    int key = Integer.parseInt(input[0]);

    Map<Integer, String> inner = outer.get(key);
    if(inner == null){
        inner = new HashMap<Integer, String>();
        outer.put(key, inner);
    }
    inner.put(Integer.parseInt(input[1]), input[2]);
}

Collections .

clone. - , .

- , , - .

+2

- . Java, JVM .

Map<Integer, Map<Integer, String>>, , Map<Pair, String>,

public final class Pair {
  private final int x;
  private final int y;
  public Pair(int x, int y) { this.x = x; this.y = y;}
}

, , . , , , , .

+1

Your code is good enough in terms of performance. Only a few things occurred to me. If the / else condition can be simplified, and you do not need to clone the map in another part (working with the pointer)

HashMap<Integer, HashMap<Integer, String>>Outer = new HashMap<Integer,   HashMap<Integer,String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
for(int i =0;i<count;i++)
{
    String input[] = br.readLine().split("\\s");

    //HashMap<Integer,String>inner = new HashMap<Integer, String>();
    int key = Integer.parseInt(input[0]);
    if(!Outer.containsKey(key))
    {
        HashMap<Integer, String> inner = new HashMap<Integer, String>();
        inner.put(Integer.parseInt(input[1]),input[2]);
        Outer.put(key, inner);
    }
    else
    {
        HashMap<Integer, String> inner = Outer.get(key);
        inner.put(Integer.parseInt(input[1]), input[2]);
    }
}
0
source

All Articles