[HD-OCP17/21-Fundamentals Pg 503, Sec. 18.1.0 - collections-framework-implementation-classes-summary]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
radupana
Posts: 28
Joined: Fri Feb 09, 2018 11:50 am
Contact:

[HD-OCP17/21-Fundamentals Pg 503, Sec. 18.1.0 - collections-framework-implementation-classes-summary]

Post by radupana »

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".

admin
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]

Post by admin »

You are right. This point should be noted in the table.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests