# Replace Stream Collect By ToList

# Description

Java 16 introduced Stream.toList() (opens new window) as a shorthand method for converting a Stream into an unmodifiable List.
This rule replaces invocations of collect(Collectors.toUnmodifiableList()) on streams by invocations of the new method stream.toList(). In case Collectors.toList() is used as a collector, the rule makes additional verifications whether the generated list is modified by the context or not. In the latter case, invocations of collect(Collectors.toList()) on streams are also replaced by invocations of toList().

Thus the code is simplified and readability is improved.

Requirements

  • Java 16

# Benefits

Removes code clutter. Improves readability.

# Tags

# Code Changes

# Using Collectors.toUnmodifiableList()

Pre

List<String> List = collection
		.stream()
		.map(function)
		.filter(predicate)
		.collect(Collectors.toUnmodifiableList());

Post

List<String> List = collection
		.stream()
		.map(function)
		.filter(predicate)
		.toList();

# Argument of Collections.unmodifiableList(...)

Pre

return Collections.unmodifiableList(collection
		.stream()
		.map(function)
		.filter(predicate)
		.collect(Collectors.toList()));

Post

return Collections.unmodifiableList(collection
		.stream()
		.map(function)
		.filter(predicate)
		.toList());

# Using Collectors.toList()

Pre

void sendMails(List<User>users) {
	List<String> emails = users.stream()
		.map(User::getMail)
		.collect(Collectors.toList());
	for (String email : emails) {
		validate(email);
		send(email);
	}
}

Post

void sendMails(List<User>users) {
	List<String> emails = users.stream()
		.map(User::getMail)
		.toList();
	for (String email : emails) {
		validate(email);
		send(email);
	}
}

# Argument of Collections.min(...)

Pre

int min = Collections.min(collection
		.stream()
		.map(function)
		.filter(predicate)
		.collect(Collectors.toList()));

Post

int min = Collections.min(collection
		.stream()
		.map(function)
		.filter(predicate)
		.toList());

# Argument of List.copyOf(...)

Pre

return List.copyOf(collection
		.stream()
		.map(function)
		.filter(predicate)
		.collect(Collectors.toList()));

Post

return List.copyOf(collection
		.stream()
		.map(function)
		.filter(predicate)
		.toList());

# Argument of list.addAll(...)

Pre

list.addAll(collection
		.stream()
		.map(function)
		.filter(predicate)
		.collect(Collectors.toList()));

Post

list.addAll(collection
		.stream()
		.map(function)
		.filter(predicate)
		.toList());

# Enhanced For Loop with Stream.collect(...)

Pre

for (String s : collection
		.stream()
		.map(function)
		.filter(predicate)
		.collect(Collectors.toList())) {
	//...
}

Post

for (String s : collection
		.stream()
		.map(function)
		.filter(predicate)
		.toList()) {
	//...
}

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 ReplaceStreamCollectByToList
First seen in jSparrow version 4.4.0
Minimum Java version 16
Remediation cost 2 min