Use isEmpty() Instead of size(); toList() #2474
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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().