在向ArrayList <hashmap <string,string>&gt; </hashmap <string,string>中的相同键名称添加不同值时,会覆盖以前的值

时间:2014-10-01 04:53:04

标签: arraylist hashmap

我正在尝试将具有相同key但不同value的项目添加到ArrayList<HashMap<String,String>>。我遇到的问题是,当我尝试添加具有相同键但值不同的值时,先前的值会被覆盖。任何人都可以建议我解决这个问题。我的代码如下:

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


       for (int i1 = 0; i1 < json.length(); i1++) {

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

                String order_id = json.getJSONObject(i1).getString(
                        "order_id");
                map.put("order_id", order_id);
                String initial_time = json.getJSONObject(i1).getString(
                        "initial_time");
                map.put("initial_time", initial_time);
                String placed_time = /* "PLACED" + "\n" */
                json.getJSONObject(i1).getString("placed_time");
                map.put("placed_time", placed_time);
                String confirmed_time = "CONFIRMED"
                        + "\n"
                        + json.getJSONObject(i1)
                                .getString("confirmed_time");
                map.put("confirmed_time", confirmed_time);
                String processed_time = json.getJSONObject(i1).getString(
                        "paid_time");
                map.put("processed_time", processed_time);
                String paid_time = json.getJSONObject(i1).getString(
                        "initial_time");
                map.put("paid_time", paid_time);
                String total = json.getJSONObject(i1).getString("total");
                map.put("total", total);
                String guest_order_type = json.getJSONObject(i1).getString(
                        "guest_order_type");
                map.put("guest_order_type", guest_order_type);
                String order_status = json.getJSONObject(i1).getString(
                        "order_status");
                map.put("order_status", order_status);
                String payment_satus = json.getJSONObject(i1).getString(
                        "payment_satus");
                map.put("payment_satus", payment_satus);
                String delivery_time = json.getJSONObject(i1).getString(
                        "delivery_time");
                map.put("delivery_time", delivery_time);
                String status = json.getJSONObject(i1).getString("status");
                map.put("status", status);
                String table_no = json.getJSONObject(i1).getString(
                        "table_no");
                map.put("table_no", "TABLE: " + table_no);
                String order_type = json.getJSONObject(i1).getString(
                        "order_type");
                map.put("order_type", order_type);
                String device_type = json.getJSONObject(i1).getString(
                        "device_type");
                map.put("device_type", device_type);


                JSONArray arr1 = json.getJSONObject(i1).getJSONArray(
                        "order_course_list");
                for (int j = 0; j < arr1.length(); j++) {

                    String sub_id = arr1.getJSONObject(j).getString(
                            "sub_id");
                    map.put(sub_id, sub_id);
                    //key_names.add(sub_id);
                    Log.i("sub id for loop", sub_id);
                    String status1 = arr1.getJSONObject(j).getString(
                            "status");
                    map.put("status1", status1);

                    String master_order_id = arr1.getJSONObject(j)
                            .getString("master_order_id");
                    map.put("master_order_id" + sub_id, master_order_id);

                    String placed_time1 = arr1.getJSONObject(j).getString(
                            "placed_time");
                    map.put("placed_time1", placed_time1);
                    String confirmed_time1 = arr1.getJSONObject(j)
                            .getString("confirmed_time");
                    map.put("confirmed_time1", confirmed_time1);

                    // map.put("master_order_id", master_order_id);
                    String processed_time1 = arr1.getJSONObject(j)
                            .getString("processed_time");
                    map.put("processed_time1", processed_time1);
                    // map.put("status", status);
                    String waiter_code = arr1.getJSONObject(j).getString(
                            "waiter_code");
                    map.put("waiter_code", waiter_code);
                    String order_type1 = arr1.getJSONObject(j).getString(
                            "order_type");
                    map.put("order_type1", order_type1);
                    // map.put("status", status);
                    String item_processed_status = arr1.getJSONObject(j)
                            .getString("item_processed_status");
                    map.put("item_processed_status", item_processed_status);

                    JSONArray arr2 = arr1.getJSONObject(j).getJSONArray(
                            "order_item_list");
                    for (int k = 0; k < arr2.length(); k++) {

                        String food_Id = arr2.getJSONObject(k).getString(
                                "food_Id");
                        map.put("food_Id", food_Id);
                        Log.i("sub id in outer loop", sub_id);
                        String food_item_id = arr2.getJSONObject(k)
                                .getString("food_item_id");
                        map.put("food_item_id", food_item_id);
                        String food_name = arr2.getJSONObject(k).getString(
                                "food_name");
                        map.put("food_name" + sub_id, food_name);
                        String food_price = arr2.getJSONObject(k)
                                .getString("food_price");
                        map.put("food_price", food_price);
                        String food_quantity = arr2.getJSONObject(k)
                                .getString("food_quantity");
                        map.put("food_quantity", food_quantity);
                        String food_item_price = arr2.getJSONObject(k)
                                .getString("food_item_price");
                        map.put("food_item_price", food_item_price);
                        String food_image = arr2.getJSONObject(k)
                                .getString("food_image");
                        map.put("food_image", food_image);
                        String food_prefids = arr2.getJSONObject(k)
                                .getString("food_prefids");
                        map.put("food_prefids", food_prefids);
                        String food_preference = arr2.getJSONObject(k)
                                .getString("food_preference");
                        map.put("food_preference", food_preference);
                        String food_preference_price = arr2
                                .getJSONObject(k).getString(
                                        "food_preference_price");
                        map.put("food_preference_price",
                                food_preference_price);
                        String ItemStatus = arr2.getJSONObject(k)
                                .getString("ItemStatus");
                        map.put("ItemStatus", ItemStatus);

                    }


                }


                temp.add(map);

3 个答案:

答案 0 :(得分:2)

试试这个..

在初始化新的地图之前,将其添加到 arraylist 中,因为如果您重新初始化该地图以前的值将会消失

temp.add(map);
map = new HashMap<String, String>();

答案 1 :(得分:1)

您正在每次循环迭代中创建新的HashMap。 因此,只有最后一张地图将被放入结果ArrayList&gt;温度。

答案 2 :(得分:0)

您所看到的是Map界面的设计行为。来自Map documentation

“将键映射到值的对象。映射不能包含重复键;每个键最多可映射一个值。”

如果需要键映射到多个值,则必须使用其他对象。例如,Guava包含许多实现MultiMap接口的对象。