0

I'm currently trying to write an XML Parser with SAX and want to save the elements of an XML file into a Hashtable, but for this I need another one in that first table ( like this ):

Hashtable<String, Hashtable<String, Set>> table;

My question is whether its possible to address the second hashtable and, if so, how do I do this?

user263980
  • 31
  • 1
  • 6
  • What's wrong with `table.get("Key1").get("Key2");`? – Sizik Mar 24 '15 at 20:56
  • The problem is that i want to change the content and not get it – user263980 Mar 24 '15 at 20:58
  • 1
    `table.get("Key1").put("Key2", mySet);`? – Sizik Mar 24 '15 at 20:59
  • it was my initial thought on how to do this but somehow i still get an error, it does not see it as a Hashtable, but instead as a Set – user263980 Mar 24 '15 at 21:03
  • Well, what error are you getting? – Sizik Mar 24 '15 at 21:05
  • "The method put( String, Hashtable< String, Set > ) in type put( String, Hashtable< String, Set > ) is not applicable for the arguments ( String, Set ) for the following line: table.put( "layout", table.get("layout").put(qName, components) ); " – user263980 Mar 24 '15 at 21:08
  • You might want to try this instead: `Hashtable layout = new Hashtable<>(); layout.put(qName, components); table.put("layout", layout);` – Sizik Mar 24 '15 at 21:13

1 Answers1

0

Do it like this:

public static void main (String[] args) throws java.lang.Exception
    {
        Map<String, Map<String, Set<Integer>>> mapOfMaps = new Hashtable<String, Map<String, Set<Integer>>>();
        Set<Integer> is = new HashSet<Integer>();
        is.add(3);
        Map<String, Set<Integer>> innerMap= new Hashtable<String, Set<Integer>>();
        innerMap.put("Your Key", is);
        mapOfMaps.put("Your Key Outer", innerMap);
        Map<String, Set<Integer>> res = mapOfMaps.get("Your Key Outer");
        Set<Integer> innerRes = innerMap.get("Your Key");
        if (innerRes.contains(3)){
            System.out.println("Hello world.");
        }
    }

The reason I recommend to store the result of the first get is that you should check for null there or do a contains beforehand (, which is more preformant, if you use it a lot).

midor
  • 5,487
  • 2
  • 23
  • 52