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

"Stream.toList()" method should be used instead of "collectors" when unmodifiable list needed

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Replace this usage of 'Stream.collect(Collectors.toList)' or 'Stream.collect(Collectors.toUnmodifiableList())' with 'Stream.toList()'
    • Highlighting:
      Hide

      invocation of collect(Collectors.toList()) or collect(Collectors.toUnmodifiableList())

      Show
      invocation of collect(Collectors.toList()) or collect(Collectors.toUnmodifiableList())
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources

      Description

      In Java 8 Streams were introduced to support chaining of operations over collections in a functional style. The most common way to save a result of such chains is to save them to some collection (usually List). To do so there is a terminal method collect that can be used with a library of Collectors. The key problem is that .collect(Collectors.toList()) actually returns a mutable kind of List while in the majority of cases unmodifiable lists are preferred. In Java 10 a new collector appeared to return an unmodifiable list: toUnmodifiableList(). This does the trick but results in verbose code. Since Java 16 there is now a better variant to produce an unmodifiable list directly from a stream: Stream.toList().

      This rule raises an issue when "collect" is used to create a list from a stream.

      Noncompliant Code Example

      List<String> list1 = Stream.of("A", "B", "C")
                                 .collect(Collectors.toList()); // Noncompliant
      
      List<String> list2 = Stream.of("A", "B", "C")
                                 .collect(Collectors.toUnmodifiableList()); // Noncompliant
      

      Compliant Solution

      List<String> list1 = Stream.of("A", "B", "C").toList(); // Compliant
      
      List<String> list2 = Stream.of("A", "B", "C")
                                 .collect(Collectors.toList()); // Compliant, the list2 needs to be mutable
      
      list2.add("X");
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                margarita.nedzelska Margarita Nedzelska
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: