Java - how to adress a Hashtable in a Hashtable

社会主义新天地 提交于 2019-12-12 06:03:13

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/29242665/java-how-to-adress-a-hashtable-in-a-hashtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!