繁体   English   中英

如何存储一个ArrayList <HashMap<String, String> &gt;具有单个Hashmap和多个Arraylist

[英]How to store an a ArrayList<HashMap<String, String>> with a single Hashmap and multiple Arraylist

我目前遇到我的代码问题

我正在尝试将一个Hashmap的值放入ArrayList>中,该值从另一个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));
    }
}

但我最终在我的数组列表中仅获得了值routeNo_set(2),address_set(2),busType_set(2)重复了3次

输出截图

任何帮助都会非常有帮助

您的问题来自以下事实:您始终在循环内使用相同的映射,并将其存储3次到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));
    }
}

之所以只获取一个值,是因为Hashmap值在循环中被覆盖,因为HashMap不允许使用重复键。因此,您总是会得到HashMap的最后一个索引值。

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

假设您只想在数组列表中添加一对键值对。

这是示例,了解有关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));
            }
         }
}

在这里检查输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM