Java-如何在哈希表中处理哈希表 [英] Java - how to adress a Hashtable in a Hashtable

查看:82
本文介绍了Java-如何在哈希表中处理哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用SAX编写XML解析器,并希望将XML文件的元素保存到哈希表中,但是为此,我需要在第一个表中再添加一个(例如):

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?

推荐答案

这样做:

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

我建议存储首次获取结果的原因是,您应该在那里检查是否为null或事先做一个包含(如果您经常使用它,它会更出色).

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

这篇关于Java-如何在哈希表中处理哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆