# Replace Equality Check with isEmpty()

# 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.

# Tags

# Code Changes

# size, length


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


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

You Want To Have Those Changes Done Automatically?

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

# Properties

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