Hi,
In the table describing the various rules of instantiating Collections, it is mentioned that the `of` and `copyOf` static factory methods for `Set` will throw an IllegalArgumentException if there are duplicates in the input.
However, I believe this is only true for the `of` method, not `copyOf`.
Here is the JavaDoc for `copyOf`:
Returns an unmodifiable Set containing the elements of the given Collection. The given Collection must not be null, and it must not contain any null elements. If the given Collection contains duplicate elements, an arbitrary element of the duplicates is preserved. If the given Collection is subsequently modified, the returned Set will not reflect such modifications.
I have also tested it on OpenJDK 21 and using `Set.copyOf("a","a")` did indeed successfully execute and generated a set containing a single "a".
[HD-OCP17/21-Fundamentals Pg 503, Sec. 18.1.0 - collections-framework-implementation-classes-summary]
Moderator: admin
-
- Posts: 28
- Joined: Fri Feb 09, 2018 11:50 am
- Contact:
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 503, Sec. 18.1.0 - collections-framework-implementation-classes-summary]
You are right. This point should be noted in the table.
thank you for your feedback!
thank you for your feedback!
Who is online
Users browsing this forum: No registered users and 3 guests