1

I want to compare two list (one is nested) for mutual exclusivity. Problem is that this code is printing false even if they have only one element in common. I need it to print false if they have both elements in common.

output I'm getting: false true false

Desired output: true true false

...
ArrayList<String> properties = new ArrayList<>(Arrays.asList("A", "B"));
ArrayList<ArrayList<String> > pairs = new ArrayList<ArrayList<String> >();

pairs.add(new ArrayList<>(Arrays.asList("A", "C")));
pairs.add(new ArrayList<>(Arrays.asList("D", "C")));
pairs.add(new ArrayList<>(Arrays.asList("A", "B")));

for(int i = 0; i< pairs.size(); i++) {
    System.out.println(Collections.disjoint(properties, pairs.get(i)));
} 
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
Michal
  • 229
  • 3
  • 11
  • 1
    Please add a description to your question, for which test data you want which result, including how the current output deviates from your expected results. – maloomeister Nov 17 '22 at 11:03
  • 1
    "Problem is that this code is returning ...." <- The code you posted doesn't contain any return statement. So I would be very surprised if it returned anything at all. – OH GOD SPIDERS Nov 17 '22 at 11:05

2 Answers2

1

So, as I understand your question now after your edit, you are making it too difficult. There is no need for Collections.disjoint() here, if you are only looking for non-equal lists. If you want to print false if you come across a List with identical contents, why not simply use equals()?

So the check becomes

for (int i = 0; i < pairs.size(); i++) {
   System.out.println(!properties.equals(pairs.get(i)));
}

printing the following output

true
true
false
maloomeister
  • 2,461
  • 1
  • 12
  • 21
0

Collection.containsAll()

I want to compare two list (one is nested) for mutual exclusivity. Problem is that this code is printing false even if they have only one element in common. I need it to print false if they have both elements in common.

If you want to make sure if two Lists having equal size contain the same elements regardless of their order, then you can make use of the method Collection.containsAll().

List<String> properties = List.of("A", "B");
List<List<String>> pairs = List.of(
    List.of("A", "C"), List.of("D", "C"), List.of("A", "B")
);
        
for (List<String> pair : pairs) {
    System.out.println(!properties.containsAll(pair));
}

Note

  • equals method of all implementations of the List interface would return true only if two lists contain the same elements in the same order (if you don't want the order to be taken into account, then equals is not suitable for this task).

  • Usage of containsAll() is justifiable only if the list on which you're invoking this method is moderate in size. In this case, it's perfectly acceptable because there are only 2 elements in the list. Otherwise, it would be performance-wise create a HashSet based on the properties list (if all elements are unique) and perform checks against this set (if there could be duplicates in the list we can generate a HashMap of element-frequencies).

  • You might want to read What does it mean to "program to an interface"?

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46