Home > Blockchain >  How to retrieve data from nested disctionary in java
How to retrieve data from nested disctionary in java

Time:04-16

I have two dictionaries added into parent dictionary. How to retrieve data from a value (which is a dictionary)

import java.util.*;

class HelloWorld {
  public static void main(String[] args) {
    int count =2;
    
    Dictionary parent_dic = new Hashtable();
    Dictionary child1 = new Hashtable();
    Dictionary child2 = new Hashtable();
    Dictionary child = new Hashtable();
    
    child1.put("1", "one");
    child1.put("2", "two");
    System.out.println("Child1"   child1);
    
    child2.put("3", "three");
    child2.put("4", "four");
    System.out.println("Child2"   child2);
    
   for(int each_c = 1; each_c <= count; each_c  ) {
       if(each_c==1) {
           child = child1;
       } else {
           child = child2;
       }
       parent_dic.put(each_c,child);
   }
   
    
    System.out.println("Parent"   parent_dic);
    System.out.println("test ::: "   parent_dic.get(1));
    
}

}

How to get value from resultant value (which is dictionary)?

Output:

Child1{2=two, 1=one}
Child2{4=four, 3=three}
Parent{2={4=four, 3=three}, 1={2=two, 1=one}}
test ::: {2=two, 1=one}

How to get value "two" from above test dictionary.

CodePudding user response:

I tried to rewrite your code in a way that made more sense to me.

I've added the use of proper types as well, using raw types in java is dangerous. The last line of my code does what I believe you want. Note the difference between using Integers as keys and Strings as keys.


    public static void main(String[] args) {
        Dictionary<Integer, Dictionary> parent_dic = new Hashtable();
        Dictionary<String, String> child1 = new Hashtable();
        Dictionary<String, String> child2 = new Hashtable();

        child1.put("1", "one");
        child1.put("2", "two");
        System.out.println("Child1"   child1);

        child2.put("3", "three");
        child2.put("4", "four");
        System.out.println("Child2"   child2);

        parent_dic.put(1, child1);
        parent_dic.put(2, child2);

        System.out.println("Parent"   parent_dic);
        System.out.println("test : "   parent_dic.get(1));
        System.out.println("two : "   parent_dic.get(1).get("2"));
    }

CodePudding user response:

You need to add generic types to your variable declarations (e.g. Dictionary<Integer, Dictionary> parent_dic = new HashTable<Integer, Dictionary>();)

Without them the compiler tries to add them in, not always successfully. The key and value pairs for your parent_dic, child1, and child2 have defaulted to Object. You can add anything to them, even a Random object or an ArrayList. If you try to access the output of your last line by adding .get("1") it won't compile, because you are trying to use get("1") on an Object.

If you add the types, like Dictionary<Integer, Dictionary> you can access the data in the child dictionaries.

  • Related