# Replace removeAll() with clear()

# Description

Simplifies the code by replacing all occurrences of removeAll() which have the current collection as parameter with clear(). Calling c.removeAll(c) to clear a collection is less clear, susceptible to errors from typos, less efficient and for some collections, might throw a ConcurrentModificationException.

# Benefits

Calling clear() instead of removeAll() makes code more clear and efficient, when at the same time less error prone.

# Tags

# Code Changes

Pre

public String testIfCollectionIsEmpty(String input){
    List<String> resultList = generateList(input);

    resultList.removeAll(resultList);

    StringBuilder sb = new StringBuilder();

    resultList.stream().forEach((s)->sb.append(s));

    return sb.toString();
}

Post

public String testIfCollectionIsEmpty(String input){
    List<String> resultList = generateList(input);

    resultList.clear();

    StringBuilder sb = new StringBuilder();

    resultList.stream().forEach((s)->sb.append(s));

    return sb.toString();
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

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

# Properties

Property Value
Rule ID CollectionRemoveAll
First seen in jSparrow version 1.0.0
Minimum Java version 1.2
Remediation cost 2 min
Links