Skip to content

Use isEmpty() Instead of size(); toList() #2474

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

Merged
merged 1 commit into from
Nov 9, 2022

Conversation

garyrussell
Copy link
Contributor

Sonar:

Collection.isEmpty() should be used to test for emptiness (java:S1155)

CODE_SMELL_MINOR

Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can be more performant. The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n).

Also

"Stream.toList()" method should be used instead of "collectors" when unmodifiable list needed (java:S6204)

CODE_SMELL_MAJOR

In Java 8 Streams were introduced to support chaining of operations over collections in a functional style. The most common way to save a result of such chains is to save
them to some collection (usually List). To do so there is a terminal method collect
that can be used with a library of Collectors. The key problem is that
.collect(Collectors.toList()) actually returns a mutable kind of List while in the majority of cases unmodifiable lists are preferred. In Java 10 a new collector appeared to return an unmodifiable list: toUnmodifiableList(). This does the trick but results in verbose code.
Since Java 16 there is now a better variant to produce an unmodifiable list
directly from a stream: Stream.toList().

Sonar:

Collection.isEmpty() should be used to test for emptiness (java:S1155)

CODE_SMELL_MINOR

Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can be more performant. The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n).

Also

"Stream.toList()" method should be used instead of "collectors" when unmodifiable list needed (java:S6204)

CODE_SMELL_MAJOR

In Java 8 Streams were introduced to support chaining of operations over collections
in a functional style. The most common way to save a result of such chains is to save
 them to some collection (usually List). To do so there is a terminal method collect
 that can be used with a library of Collectors. The key problem is that
`.collect(Collectors.toList())` actually returns a mutable kind of List
while in the majority of cases unmodifiable lists are preferred.
In Java 10 a new collector appeared to return an unmodifiable list:
`toUnmodifiableList()`. This does the trick but results in verbose code.
 Since Java 16 there is now a better variant to produce an unmodifiable list
 directly from a stream: Stream.toList().
@artembilan artembilan added this to the 3.0.0 milestone Nov 9, 2022
@artembilan artembilan merged commit bdf017d into spring-projects:main Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants