Using both Java 8 and Java 11, consider the following TreeSet
with a String::compareToIgnoreCase
comparator:
final Set<String> languages = new TreeSet<>(String::compareToIgnoreCase);
languages.add("java");
languages.add("c++");
languages.add("python");
System.out.println(languages); // [c++, java, python]
When I try to remove the exact elements present in the TreeSet
, it works: all of those specified are removed:
languages.removeAll(Arrays.asList("PYTHON", "C++"));
System.out.println(languages); // [java]
However, if I try to remove instead more than is present in the TreeSet
, the call doesn't remove anything at all (this is not a subsequent call but called instead of the snippet above):
languages.removeAll(Arrays.asList("PYTHON", "C++", "LISP"));
System.out.println(languages); // [c++, java, python]
What am I doing wrong? Why does it behave this way?
Edit: String::compareToIgnoreCase
is a valid comparator:
(l, r) -> l.compareToIgnoreCase(r)