如何在Java中注入嵌套的map.entries?

时间:2014-07-10 14:33:45

标签: spring collections map

我正在尝试获取第二个条目的值并且具有isues。在此先感谢您的帮助。

这是我的代码:

<util:map id="externalUrlMap" map-class="java.util.HashMap">
    <entry key="privacy">        
        <util:map map-class="java.util.HashMap">
            <entry key="en" value="${exit.privacy.url.en}"/>
            <entry key="fr" value="${exit.privacy.url.fr}"/>        
        </util:map>        
    </entry>
   <entry key="logout">        
        <util:map map-class="java.util.HashMap">
            <entry key="en" value="${exit.logout.url.en}"/>
            <entry key="fr" value="${exit.logout.url.fr}"/>        
        </util:map>        
    </entry>
</util:map>

和控制器

String urlType = "privacy";
    // Using Iterator to loop externalUrlMap
    Iterator<Map.Entry<String, Entry<String, String>>> entries = externalUrlMap.entrySet().iterator();  

    // Start Iterator 
    while (entries.hasNext()) {             
          Map.Entry<String, List<String>> entry = entries.next();

          if (entry.getKey().equalsIgnoreCase(urlType)){
              String key = entry.getKey();
              //Object value = entry.getValue().en; 
              List<String> list =  entry.getValue();
                //first item
                String first = list.get(0);
                //second item  
                 String second = list.get(1);

              System.out.println(key + "" + ""+ first +""+ second );
          }
    }

这是我得到的错误 类型不匹配:无法转换为Iterator&gt;到Iterator&gt;&gt; 非常感谢。

1 个答案:

答案 0 :(得分:0)

测试我做了以下简约设置。访问值direkt没问题。

import java.util.Map;

import org.springframework.context.support.GenericXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        GenericXmlApplicationContext context = new GenericXmlApplicationContext("/application-context.xml");
        Map<String, Map<String, String>> externalUrlMap = (Map<String, Map<String, String>>)context.getBean("externalUrlMap");

        System.out.println(externalUrlMap.get("privacy").get("en"));
        System.out.println(externalUrlMap.get("privacy").get("fr"));
        System.out.println(externalUrlMap.get("logout").get("en"));
        System.out.println(externalUrlMap.get("logout").get("fr"));


    }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd         
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<bean 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="location">
            <value>url.properties</value>
        </property>
    </bean>


    <util:map id="externalUrlMap" map-class="java.util.HashMap">
    <entry key="privacy">        
        <util:map map-class="java.util.HashMap">
            <entry key="en" value="${exit.privacy.url.en}"/>
            <entry key="fr" value="${exit.privacy.url.fr}"/>        
        </util:map>        
    </entry>
   <entry key="logout">        
        <util:map map-class="java.util.HashMap">
            <entry key="en" value="${exit.logout.url.en}"/>
            <entry key="fr" value="${exit.logout.url.fr}"/>        
        </util:map>        
    </entry>
</util:map>

</beans>