0

I want to get the difference of 2 Lists of Strings.

I found CollectionUtils does that. However I am getting a compilation warning.

Collection<String> a = new ArrayList<>();
Collection<String> b = new ArrayList<>();
Collection<String> c = CollectionUtils.subtract(a, b);

Which produces:

Type safety: The expression of type Collection needs unchecked conversion to conform to Collection<String>

Is there any way to use this library and not get this warning? Apart from @SupressWarnings.

Mayday
  • 4,680
  • 5
  • 24
  • 58
  • 4
    If you mean `org.apache.commons.collections.CollectionUtils.subtract(Collection, Collection)`, then that's because it returns the raw `Collection` type. This is easy enough to implement using `List.removeAll` – ernest_k Feb 11 '19 at 09:35
  • 5
    You've linked to the package `org.apache.commons.collections4` but you're probably **actually** using the package [`org.apache.commons.collections`](https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.2/org/apache/commons/collections/CollectionUtils.html#subtract(java.util.Collection,%20java.util.Collection)), which returns the raw type. The method you've linked to would not give that warning. – Michael Feb 11 '19 at 09:36
  • 3
    `Collection c = new ArrayList<>(a); c.removeAll(b);`, and you don't need the 3rdparty class. – ernest_k Feb 11 '19 at 09:41

0 Answers0