Remove Only One Item from a List (Android)

Multi tool use


Remove Only One Item from a List (Android)
I have a list: (x, y, y,z)
(x, y, y,z)
I have another list: (x, y)
(x, y)
You want to remove the items from the 1st list that belong also in the 2nd list.
Ex: I want the result to be (y,z)
(y,z)
How do I do this?
EDIT: If I use removeAll. The example list returns (z)
Does order matter? For example, if the second list was (y, x) would you want the same outcome?
– Tyler V
4 mins ago
The result will be (z) not (y,z)
– mTak
3 mins ago
Guys, its simple. I don't want it to remove y twice.
– Ron Arel
2 mins ago
And the result is z
– Ron Arel
2 mins ago
1 Answer
1
You can use remove all
List<String> one = new ArrayList<String>();
one.add("x");
one.add("y");
one.add("y");
one.add("z");
List<String> two = new ArrayList<String>();
two.add("x");
two.add("y");
one.removeAll(two);
System.out.println(one);
If I removeAll. The example list returns (z)
– Ron Arel
8 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You want to remove the items from the 1st list that belong also in the 2nd list. Edit your question.
– mTak
7 mins ago