簡體   English   中英

Java-如何在哈希表中處理哈希表

[英]Java - how to adress a Hashtable in a Hashtable

我目前正在嘗試使用SAX編寫XML解析器,並希望將XML文件的元素保存到Hashtable中,但是為此,我需要在第一個表中再添加一個(如下所示):

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

我的問題是是否有可能解決第二個哈希表,如果可以,我該怎么做?

像這樣做:

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.");
        }
    }

我建議存儲第一個get結果的原因是,您應該在那里檢查是否為null或事先做一個contains(如果使用很多,它會更出色)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM