I've just encountered a crash caused by adding duplicate entries to the java.util.Set.of(...)
method added in Java 9.
Reading the method's documentation it is clear that such a crash can occur (shame on me), but I find this behavior counter-intuitive compared to the familiar behavior of a Set
to prevent duplicate entries 'behind the scenes'.
Set<String> set;
// set will only contain the one element "foo"
set = new HashSet<>();
set.add("foo");
set.add("foo");
// again only one element "foo"
set = new HashSet<>(Lists.of("foo", "foo"));
// crash, although it looks like a shortcut for the familiar behavior above
set = Set.of("foo", "foo");
Intuitively I'd expect the method to create an immutable Set of the input parameters after applying the Set's 'magic'.
What was / may have been the reasoning behind this design choice?