# Replace Equality Check with isEmpty()

# Properties

Property Value
Rule ID UseIsEmptyOnCollections
First seen in jSparrow version 2.0.2
Minimum Java version 6
Remediation cost 2 min
Links S1155

# Description

This rule replaces comparisons of length() or size() with 0 with a call to isEmpty(). Any occurrences of such a comparison will be replaced, regardless of surrounding control structure.

# Benefits

The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n). Thus, using this rule provides performance benefits.

# Code Changes

# size, length

Pre

Map<String, String> m = new HashMap<>();
if(m.size() == 0);
String s = "";
if(s.length() == 0);

Post

Map<String, String> m = new HashMap<>();
if(m.isEmpty());
String s = "";
if(s.isEmpty());

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow versions:

# Tags

1
default
You & jSparrow
default

Hey there! May I help you? 😊