There seems to be a better place to use the Collections.of() or Stream API than the Collections.unmodifiableXXX() method. Therefore, I suggest an alternative method in the following cases:
-
If you needs information that is copied rather than the original In some cases, a new collection object was created within a function and the created object was returned through an UnmodifiableXXX() method. Removed unnecessary object creation through Collection.copyOf() method.
-
When a static block is used to create an immutable collection upon initialization Removed unnecessary static blocks by using collection.of() or Stream api.
-
When creating a new Collection object and putting content through an unnecessary for statement Remove unnecessary object creation and for statement through Stream api
Comment From: sbrannen
This has been merged into main
in ba136dcf40123445aa959786cdc006112d35774a and revised (and partially reverted in 95f3337bb526e639f3362d5158a15a4c6ca36958).
For details on what needed to be reverted and why, please see the latter commit.
Thanks for the contribution