Rahul Ravindran
Rahul Ravindran

Reputation: 304

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

I am currently facing a problem with my code

I am trying to put in a ArrayList> the values of a Hashmap which gets its values from another arraylist

Here is the code :

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

but I am ending up getting only the value routeNo_set(2) ,address_set(2), busType_set(2) in my arraylist repeated 3 times

output screenshot

any help would be very helpfull thanks in Advance

Upvotes: 0

Views: 3582

Answers (2)

Ravikumar
Ravikumar

Reputation: 901

The reason why you are getting only one value is that the Hashmap values are overwritten in the loop because duplicate keys are not allowed in HashMap.So you will always get the last index values in your hashmap.

So if want one key with different values you can use HashMap<String, ArrayList<String>>.

Assuming that you want only one key value pair which you want to add in your arraylist.

Here is the example, Read about 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));
            }
         }
}

Check output here

Upvotes: 1

Your problem come from the fact that you're always using the same map inside your loop, and you store it three times into your ArrayList.

That's why you're getting the same results, because it's the same map and the put() method replace the old value for the key if the provided key already exists in the map.

You have to instanciate a new map each time you go through the loop.

The following code should work :

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

Upvotes: 1

Related Questions