Skip to content

CollectorMutability: Suggest immutable Guava alternative  #954

@rickie

Description

@rickie

Problem

In checkstyle/checkstyle#14229 we are enabling the CollectorMutability check in the Checkstyle repository. The Checkstyle team tries to minimize the dependencies they have on Guava. Therefore, the rewrite to Immutable{List,Map,Set}.toImmutable{List,Map,Set}() is not desirable. An alternative would be to use the Collectors.toUnmodifiable{List,Map,Set}(). This is a good alternative as this also clearly communicates that a list is used that should not be modified.

Description of the proposed new feature

  • Support a stylistic preference.
  • Avoid a common gotcha, or potential problem.
  • Improve performance.

I would like to rewrite the following code:

.collect(Collectors.toSet());

to:

.collect(Collectors.toUnmodifiableSet());

Considerations

Two things:

  • As @Stephan202 notes, this should take into account that this rewrite is only valid when JDK > 9.
  • This XXX in ThirdPartyLibrary should be resolved as well.

Participation

  • I am willing to submit a pull request to implement this improvement.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions