Back to Intellij Community

ListIndexOfReplaceableByContains

java/java-impl/resources/inspectionDescriptions/ListIndexOfReplaceableByContains.html

2025.3-rc-2424 B
Original Source

Reports any List.indexOf() expressions that can be replaced with the List.contains() method.

Example:

boolean hasEmptyString(List<String> list) {
    // Warning: can be simplified
    return list.indexOf("") >= 0;
  }

The provided quick-fix replaces the indexOf call with the contains call:

boolean hasEmptyString(List<String> list) {
    // Quick-fix is applied
    return list.contains("");
  }