I have three lists of strings. Now I need to make sure that an element is present in only one of those three list. I don't want to have duplicates across the lists. You can assume that there are no duplicates within each list.
I tried to use the removeIf()
and its serving my purpose. Im sure that is not the best way to do it. Is there any other way to do it in Java 8+?
The Priority for the lists is list1 > list2 > list3
List<String> list1 = Stream.of("23","45","655","43","199").collect(Collectors.toList());
List<String> list2 = Stream.of("13","23","54","655","111","13").collect(Collectors.toList());
List<String> list3 = Stream.of("76","45","33","67","43","13").collect(Collectors.toList());
list2.removeIf(i->list1.contains(i));
list3.removeIf(i->list1.contains(i) || list2.contains(i));
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
The output is below which is expected
[23, 45, 655, 43, 199]
[13, 54, 111, 13]
[76, 33, 67]