Replace with .lengthCompare warning

IntelliJ continues to offer replace .length == Xwith .lengthCompare(X) == 0. Why is it better? They did not quite understand, since the proposed changes are more detailed.

enter image description here

+4
source share
1 answer

More efficient .

Since it lengthis an operation linearfor some collections, such as List, for execution, x.length == 3you must first calculate the length, and then compare it with the value. On the other hand, it .lengthComparewill complete the length calculation if it turns out that the comparison is no longer so.

+6
source

Source: https://habr.com/ru/post/1694620/


All Articles