Reputation:
I am trying to add items with same key
but with differenent value
into an ArrayList<HashMap<String,String>>
. The problem I am encountering is that when I try to add values with the same key but different value, the previous values get overwritten. Can anyone suggest me a solution to this problem. My code is as below:
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);
Upvotes: 0
Views: 475
Reputation: 2664
What you are seeing is designed behavior of Map interface. From Map documentation:
"An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value."
If you need a key to map to multiple values you have to use some other object. For example Guava contains many objects implementing MultiMap interface.
Upvotes: 0
Reputation: 916
You are creating new HashMap on EVERY loop iteration. So, only last map will be putted in the result ArrayList> temp.
Upvotes: 1
Reputation: 24853
Try this..
Before initializing new map add that into arraylist because if you reinitialize that map that previous values will gone
temp.add(map);
map = new HashMap<String, String>();
Upvotes: 2