# 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();
}
🛠️ Auto-refactor Available
You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.