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

Use a Java Refactoring Tool

Automate this Refactoring system-wide

You can apply this refactoring for free with the jSparrow Eclipse IDE plug-in.
Install the plug-in for Eclipse IDE: Eclipse Marketplace.

a drawn cute bird pointing at a graph that shows positive results

# Properties

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