Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CollectorMutability: Suggest immutable Guava alternative #954

Open
2 of 4 tasks
rickie opened this issue Jan 8, 2024 · 0 comments
Open
2 of 4 tasks

CollectorMutability: Suggest immutable Guava alternative #954

rickie opened this issue Jan 8, 2024 · 0 comments

Comments

@rickie
Copy link
Member

rickie commented Jan 8, 2024

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant