# 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

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());

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

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