简体   繁体   中英

Java - how to adress a Hashtable in a Hashtable

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM