Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3815

"Collection.removeIf" should be used instead of "Iterator.remove"

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Use "Collection.removeIf" instead of Iterator.remove
    • Highlighting:
      Hide

      remove call

      Show
      remove call
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources
    • Implementation details:

      Description

      Collection.removeIf is more readable and less verbose than using the Iterator.remove idiom. It might also be more performant in some cases, particularly for ArrayList instances.

      Noncompliant Code Example

      for (Iterator it = items.iterator(); it.hasNext();) {
        if (this.predicate(it.next())) {
          it.remove(); 
        } 
      }
      

      Compliant

      items.removeIf(this::predicate);
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            nicolas.peru Nicolas Peru
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: