如何存储ArrayList <hashmap <string,string =“”>&gt;使用单个Hashmap和多个Arraylist

时间:2016-07-02 13:21:57

标签: java android arrays arraylist hashmap

我目前遇到代码问题

我想放入一个ArrayList&gt; Hashmap的值从另一个arraylist获取其值

以下是代码:

ArrayList<HashMap<String, String>> AL_route_bus_collection_a = new    ArrayList<HashMap<String,String>>();

HashMap<String,String>  HM_route_bus_collection_a  = new HashMap<String, String>();


for(int i = 0;i<routeNo_set.size();i++ ) {

    HM_route_bus_collection_a.put("route_no", routeNo_set.get(i));
    HM_route_bus_collection_a.put("address", address_set.get(i));
    HM_route_bus_collection_a.put("bus_type", busType_set.get(i));

    AL_route_bus_collection_a.add(HM_route_bus_collection_a);
}
for (HashMap<String, String> hashMap : AL_route_bus_collection_a) {
    System.out.println(hashMap.keySet());
    for (String key : hashMap.keySet()) {
        System.out.println(hashMap.get(key));
    }
}

但我最终只得到了我的arraylist中重复3次的值routeNo_set(2),address_set(2),busType_set(2)

output screenshot

任何帮助都会非常有帮助 在此先感谢

2 个答案:

答案 0 :(得分:0)

你的问题来自于你总是在你的循环中使用相同的地图,并将它存储在你的ArrayList中三次。

这就是为什么你得到相同的结果,因为它是相同的地图,如果提供的密钥已经存在于地图中,则put()方法会替换密钥的旧值。

每次循环时都必须实例化新地图。

以下代码应该有效:

ArrayList<HashMap<String, String>> AL_route_bus_collection_a = new    ArrayList<HashMap<String,String>>();

for(int i = 0;i<routeNo_set.size();i++ ) {

    HashMap<String,String>  HM_route_bus_collection_a  = new HashMap<String, String>();
    HM_route_bus_collection_a.put("route_no", routeNo_set.get(i));
    HM_route_bus_collection_a.put("address", address_set.get(i));
    HM_route_bus_collection_a.put("bus_type", busType_set.get(i));

    AL_route_bus_collection_a.add(HM_route_bus_collection_a);
}
for (HashMap<String, String> hashMap : AL_route_bus_collection_a) {
    System.out.println(hashMap.keySet());
    for (String key : hashMap.keySet()) {
        System.out.println(hashMap.get(key));
    }
}

答案 1 :(得分:0)

您只获得一个值的原因是Hashmap值会在循环中被覆盖,因为HashMap中不允许使用重复键。因此,您将始终获得最后一个索引值HashMap中。

因此,如果想要一个具有不同值的键,您可以使用HashMap<String, ArrayList<String>>

假设您只想要在arraylist中添加一个键值对。

以下是示例,阅读HashMap

    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<HashMap<String, String>> AL_route_bus_collection_a = new ArrayList<HashMap<String,String>>();

        HashMap<String,String>  HM_route_bus_collection_a  = new HashMap<String, String>();

        List<String> routeNo_set = new ArrayList<String>();
        routeNo_set.add("first");
        routeNo_set.add("second");
        routeNo_set.add("third");

        List<String> address_set = new ArrayList<String>();
        address_set.add("A");
        address_set.add("B");
        address_set.add("C");

        List<String> busType_set = new ArrayList<String>();
        busType_set.add("1");
        busType_set.add("2");
        busType_set.add("3");

        for(int i = 0;i<routeNo_set.size();i++ ) {

            HM_route_bus_collection_a.put("route_no", routeNo_set.get(i));
            HM_route_bus_collection_a.put("address", address_set.get(i));
            HM_route_bus_collection_a.put("bus_type", busType_set.get(i));

            AL_route_bus_collection_a.add(HM_route_bus_collection_a);

            HM_route_bus_collection_a = new HashMap<String, String>();
          }
        for (HashMap<String, String> hashMap : AL_route_bus_collection_a) {
            System.out.println(hashMap.keySet());
            for (String key : hashMap.keySet()) {
                System.out.println(hashMap.get(key));
            }
         }
}

检查输出here