【问题标题】:Create Multidimensional HashMap in java在java中创建多维HashMap
【发布时间】:2014-02-27 14:59:15
【问题描述】:

我是java新手,我有这个代码

...
NodeList indexEntryList = sourceDoc.getElementsByTagName("in.ar");  //this is a xml tag
for (int i = 0; i < indexEntryList.getLength(); i++) {
    ...
}
...

我必须创建一个 HashMap(或类似的东西),在其中保存作为节点属性的字符串和具有相同属性的所有节点的列表。

我认为是这样的:

Map<String, ArrayList<Node>> indexCategories = new HashMap<String, ArrayList<Node>>();

但是在每次for中,我不知道如何在Map的所有key中搜索,并将新Node添加到列表中,如果key不存在,则在Map中创建新项目。

【问题讨论】:

标签: java arraylist hashmap xmlnode


【解决方案1】:

使用Map#containsKey() 搜索键是否存在,Map#get() 用于获取集合(如果存在),Map#put() 用于存储新创建的映射。顺便说一句,所有内容都可以在Map API 中找到正确记录。

【讨论】:

    【解决方案2】:

    如果您查看 NodeList 的文档,您会发现它只有两种方法:getLength()(您已经使用过)和Node item(int index)

    所以你的循环将是:

    for (int i = 0; i < indexEntryList.getLength(); i++) {
        Node node = indexEntryList.item(i);
        // do something with node
    }
    

    ...你想要对节点做的,就是找到它的属性。

     NamedNodeMap attributes = node.getAttributes();
     if(attributes != null) {
           for(int j=0;j < attributes.getLength(); j++) {
               Node attribute = attributes.item(j);
               // do something with node and attribute
           }
     }
    

    ...那么你想对你的属性和它的节点做什么呢?我不确定,但我认为您的意图是 map.get(attributeName) 返回包含该元素的节点列表。

    如果是这样:

      // get the list for this element, or create one
      List list = map.get(attribute.getName());
      if(list == null) {
         list = new ArrayList();
         map.put(attribute.getName(), list);
      }
      // add the node we're working with to that list
      list.add(node);
    

    这里有几点说明:

    • 使用 Set 可能比使用 List 更好,因为最终可能会多次将同一个节点添加到列表中。
    • 我真的建议将这些块中的每一个都放入一个单独的方法中,相互调用一个-也就是说,在我放置do something... 的位置进行方法调用。这为您提供了更易于理解和测试的更小的代码块;这也意味着您可以在两个“for”循环中调用循环计数器“i”。

    【讨论】:

      【解决方案3】:

      试试这个...

       Map<String, ArrayList<Node>> indexCategories = new HashMap<String, ArrayList<Node>>();
      
       public void addNode(string key, Node node) {
           List<Node> nodes;
      
           if(indexCategories.containsKey(key)) {
              nodes = indexCategories.get(key);
           } 
           else {
              nodes = new ArrayList<Node>();
           }
      
           nodes.add(node);
      
           indexCategories.put(key, node);
       }
      

      【讨论】:

        【解决方案4】:

        你可以使用类似的东西:

            String key = nodeAttribute;
            if (!indexCategories.containsKey(key)) {
                indexCategories.put(key, new ArrayList<Node>());
            }
            indexCategories.get(key).add(node);
        

        【讨论】:

          【解决方案5】:

          你可以试试这样的

              NodeList indexEntryList = sourceDoc.getElementsByTagName("in.ar");  //this is a xml tag
              for (int i = 0; i < indexEntryList.getLength(); i++) {
                   Node oneItem = indexEntryList.item(i);
                   String someString = "xxx or however you obtain the string";
                   ArrayList<Node> listOfNodes = indexCategories.get(someString);
                   if (listOfNodes == null) {
                       listOfNodes = new ArrayList<Node>();
                   }
                   listOfNodes.add(oneItem);
              }
          

          【讨论】:

            猜你喜欢
            • 2021-09-23
            • 1970-01-01
            • 2011-11-09
            • 1970-01-01
            • 2011-05-23
            • 2013-08-21
            • 1970-01-01
            • 2014-09-17
            • 2014-02-26
            相关资源
            最近更新 更多