IDEA-170626 Suggest to use Stream.peek instead of Stream.map if the lambda parameter is not reassigned during the operation.

This commit is contained in:
Tagir Valeev
2017-04-18 17:57:29 +07:00
parent 208f1a3191
commit 1c312d3e13
7 changed files with 136 additions and 6 deletions
@@ -0,0 +1,14 @@
// "Replace with 'peek'" "true"
import java.util.List;
public class Main {
void test(List<String> list) {
// hello
/* in return */
long count = list.stream()
.peek(System.out::println)
.count();
System.out.println(count);
}
}
@@ -0,0 +1,15 @@
// "Replace with 'peek'" "true"
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class Main {
void test() {
AtomicInteger counter = new AtomicInteger();
int[] ints = IntStream.range(0, 100)
.filter(x -> x % 3 == 0)
.peek((x -> counter.incrementAndGet()))
.toArray();
System.out.println(counter.get());
}
}
@@ -0,0 +1,16 @@
// "Replace with 'peek'" "true"
import java.util.List;
public class Main {
void test(List<String> list) {
long count = list.stream()
.ma<caret>p(e -> {
System.out.println(e);
// hello
return /* in return */ e;
})
.count();
System.out.println(count);
}
}
@@ -0,0 +1,18 @@
// "Replace with 'peek'" "true"
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class Main {
void test() {
AtomicInteger counter = new AtomicInteger();
int[] ints = IntStream.range(0, 100)
.filter(x -> x % 3 == 0)
.m<caret>ap((x -> {
counter.incrementAndGet();
return x;
}))
.toArray();
System.out.println(counter.get());
}
}
@@ -0,0 +1,16 @@
// "Replace with 'peek'" "false"
import java.util.stream.IntStream;
public class Main {
void test() {
int[] ints = IntStream.range(0, 100)
.filter(x -> x % 3 == 0)
.m<caret>ap(x -> {
x++;
return x;
})
.toArray();
System.out.println(ints.length);
}
}