0

This might look like a duplicate but it's not, read through the end please.
Some context : I'm building an android app that connect to an api. There's a checkbox of items "finishes" applied to a furniture object, when I'm updating the the finishes I must send back to the backend the finishes(checkbox)I want to delete. finishes_to_be_removed: [] and pass there ids in the array, while adding finishes is finishes: []. The problem is, I want to find a way to diff the the two list generated from the checkboxes, the old one and the updated one. So I can populate the finishes_to_be_removed: [].

I cannot use Java 8 Stream because my API level min is 15.

When I try list.contains(list2), it tells "List may not contains object of Type List.

I want to return items that are in the first list but that are not on the second list only, not a 1:1 comparison.

Dr4ke the b4dass
  • 1,184
  • 2
  • 17
  • 40

1 Answers1

1

List.contains() takes an element of the same type as the List, i.e. a List<String> lets you call list.contains("some string"), but you cannot pass another List<String> into .contains().

An example function that could work looks like:

public boolean <T> listContains(List<T> list1, List<T> list2) {
    for (T t : list1) {  // iterate through all elements of list1

        if (list2.contains(list1)) return true;  // check to see if list2 contains each element

    }

    return false;  // return false if no matches found
}

EDIT

After seeing your edit, if you want to get only the elements that are in the first list, but not in the second list, you can use:

list1.removeIf(item -> list2.contains(item));

This removes all items from list1 that are contained in list2

cameron1024
  • 9,083
  • 2
  • 16
  • 36