1

I have an two array lists with multiple values at single index. I am adding the values like below:

ArrayList<String> list1 = new ArrayList<String>();
list1.addAll(Arrays.asList("iFRa1wVJ","Breakfast","7:00am","8:30am"));
list1.addAll(Arrays.asList("JyvmCZhw","Lunch","1:00pm","3:00pm"));
list1.addAll(Arrays.asList("HR6ovPjE","Dinner","7:30pm","9:30pm"));


ArrayList<String> list2 = new ArrayList<String>();
list2.addAll(Arrays.asList("iFRa1wVJ","Breakfast1","9:00am","10:30am"));
list2.addAll(Arrays.asList("JyvmCZhw","Lunch","2:00pm","3:00pm"));
list2.addAll(Arrays.asList("HR6ovPjE","Dinner","8:30pm","9:30pm"));

Expected result is as below:

   iFRa1wVJ,Breakfast,7:00am,8:30am --> Breakfast1 is overwriiten as Breakfast from list1
   JyvmCZhw,Lunch,1:00pm,3:00pm --> 2:00pm is overwriiten as 1:00pm from list1
   HR6ovPjE,Dinner,7:30pm,9:30pm --> 8:30pm is overwriiten as 7:30pm from list1

I need to compare the values of each index and update the second list with the values of first list. How to do the comparisons of the list in each index. Any help will appreciated. Thanks.

4
  • 2
    Can you share the expected result? It would make the question much clearer. Commented Mar 4, 2017 at 7:20
  • Okay will update the expected answer Commented Mar 4, 2017 at 7:21
  • you don't have multiple values at a single index you only have single values at each index Commented Mar 4, 2017 at 7:31
  • use android.support.v7.util.DiffUtil: "DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one." Commented Mar 4, 2017 at 7:34

2 Answers 2

2

It sounds as though you'd be better off using a Map or Set instead of a List. For example:

class Meal {
    //You may want to store times in a different format, i.e. a long in millisecond
    private final String name, startTime, endTime;

    Meal(String name, String startTime, String endTime) {
        this.name = name;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    //getName(), getStartTime(), getEndTime()...

}

And in your map...

final Map<String, Meal> map = new HashMap<>();
map.put("iFRa1wVJ", new Meal("Breakfast","7:00am","8:30am"));
map.put("iFRa1wVJ", new Meal("Breakfast","9:00am","10:30am"));

The second "Breakfast" will replace the first as they share the same key.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay will try this
1

You wanted two array lists with multiple values at each index, but in your original code ArrayList<String> is just an array with a single String object at each index. So I changed the data structure to hold a list of Strings. If all you really want to do is update each index with the value at the corresponding index of list1 you can just update everything at the index.

ArrayList<List<String>> list1 = new ArrayList<List<String>>();
list1.add(Arrays.asList("iFRa1wVJ", "Breakfast", "7:00am", "8:30am"));
list1.add(Arrays.asList("JyvmCZhw", "Lunch", "1:00pm", "3:00pm"));
list1.add(Arrays.asList("HR6ovPjE", "Dinner", "7:30pm", "9:30pm"));

ArrayList<List<String>> list2 = new ArrayList<List<String>>();
list2.add(Arrays.asList("iFRa1wVJ", "Breakfast1", "9:00am", "10:30am"));
list2.add(Arrays.asList("JyvmCZhw", "Lunch", "2:00pm", "4:00pm"));
list2.add(Arrays.asList("HR6ovPjE", "Dinner", "8:30pm", "9:30pm"));

for (int i = 0; i < list1.size(); i++) {
    list2.set(i, list1.get(i));
}

System.out.println(list1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.