StreamToLoopInspection: support groupingBy(..., counting()/summingInt()/summingLong()/summingDouble()/mapping(summing...))

This commit is contained in:
Tagir Valeev
2017-01-24 17:54:30 +07:00
parent 80e09a9e59
commit 2d57e55143
40 changed files with 210 additions and 34 deletions

View File

@@ -6,7 +6,7 @@ import java.util.Set;
public class Main {
private static long test() {
long count = 0;
long count = 0L;
Set<Integer> uniqueValues = new HashSet<>();
long toSkip = 1;
for (Integer integer : new Integer[]{1, 2, 3, 2, 3}) {

View File

@@ -5,7 +5,7 @@ import java.util.stream.Collectors;
public class Main {
public static long test(List<String> strings) {
long count = 0;
long count = 0L;
for (String s : strings) {
if (!s.isEmpty()) {
count++;

View File

@@ -7,7 +7,7 @@ import java.util.stream.Collectors;
public class Main {
public static Double test(List<String> strings) {
double sum = 0;
double sum = 0.0;
for (String string : strings) {
if (string != null) {
sum += string.length();

View File

@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public void test(List<String> list) {
long x = 0;
long x = 0L;
for (String s : list) {
x++;
}

View File

@@ -7,7 +7,7 @@ public class Main {
}
public long test(List<Count> count) {
long result = 0;
long result = 0L;
for (Count count1 : count) {
result++;
}

View File

@@ -5,7 +5,7 @@ import java.util.List;
public class Main {
public long test(List<String> list) {
if(!list.isEmpty()) {
long count = 0;
long count = 0L;
for (String s : list) {
count++;
}

View File

@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public long test(List<String> list) {
long count = 0;
long count = 0L;
for (String s : list) {
count++;
}

View File

@@ -6,7 +6,7 @@ import java.util.Set;
public class Main {
public long test(List<String> list) {
long count = 0;
long count = 0L;
Set<String> uniqueValues = new HashSet<>();
for (String s : list) {
if (uniqueValues.add(s)) {

View File

@@ -4,7 +4,7 @@ import java.util.Arrays;
public class Main {
private static long countInRange(int... input) {
long count = 0;
long count = 0L;
for (int x : input) {
if (x > 0) {
if (x < 10) {

View File

@@ -5,7 +5,7 @@ import java.util.Arrays;
public class Main {
private static long countInRange(int... input) {
int x = 1;
long count = 0;
long count = 0L;
for (int y : input) {
if (y > 0) {
if (y < 10) {

View File

@@ -6,7 +6,7 @@ public class Main {
private static long countInRange(int... input) {
int x = 1;
int y = 2;
long count = 0;
long count = 0L;
for (int i : input) {
if (i > 0) {
if (i < 10) {

View File

@@ -7,7 +7,7 @@ public class Main {
int x = 1;
int y = 2;
int i = 3;
long count = 0;
long count = 0L;
for (int x1 : input) {
if (x1 > 0) {
if (x1 < 10) {

View File

@@ -6,7 +6,7 @@ public class Main {
private static long countInRange(int... input) {
int x = 1;
int i = 3;
long result = 0;
long result = 0L;
for (int count : input) {
if (count > 0) {
if (count < 10) {

View File

@@ -6,7 +6,7 @@ import java.util.stream.Stream;
public class Main {
private static long test(List<? extends String> list) {
long count = 0;
long count = 0L;
for (Object o : Arrays.asList(0, null, "1", list)) {
for (Object o1 : Arrays.asList(o)) {
for (Object o2 : Arrays.asList(o1)) {

View File

@@ -8,7 +8,7 @@ import static java.util.Arrays.asList;
public class Main {
private static long test(List<List<String>> nested) {
long count = 0;
long count = 0L;
for (List<String> names : nested) {
Set<String> uniqueValues = new HashSet<>();
for (String name : names) {

View File

@@ -5,7 +5,7 @@ import java.util.*;
public class Main {
private static long test(Map<String, List<String>> strings) {
long count = 0;
long count = 0L;
for (Map.Entry<String, List<String>> e : strings.entrySet()) {
if (!e.getKey().isEmpty()) {
String sInner = e.getKey();

View File

@@ -7,7 +7,7 @@ public class Main {
static Predicate<String> nonEmpty = s -> s != null && !s.isEmpty();
private static long test(List<String> strings) {
long count = 0;
long count = 0L;
for (String string : strings) {
if (nonEmpty.test(string)) {
count++;

View File

@@ -6,7 +6,7 @@ import java.util.function.Predicate;
public class Main {
private static long test(List<Predicate<String>> predicates, List<String> strings) {
long count = 0;
long count = 0L;
for (Predicate<String> pred : predicates) {
if (pred != null) {
for (String string : strings) {

View File

@@ -0,0 +1,15 @@
// "Replace Stream API chain with loop" "true"
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Integer, Long> map = new HashMap<>();
for (String s : list) {
map.merge(s.length(), 1L, Long::sum);
}
}
}

View File

@@ -0,0 +1,16 @@
// "Replace Stream API chain with loop" "true"
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Integer, Integer> map3 = new HashMap<>();
for (String s : list) {
String trim = s.trim();
map3.merge(s.length(), trim.length(), Integer::sum);
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace Stream API chain with loop" "true"
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Integer, Double> map4 = new HashMap<>();
for (String s : list) {
map4.merge(s.length(), (double) s.length(), Double::sum);
}
}
}

View File

@@ -5,7 +5,7 @@ import java.util.List;
public class Main {
private static long countNonEmpty(List<String> input) {
long count = 0;
long count = 0L;
for (String str : input) {
String trim = str.trim();
if (!trim.isEmpty()) {

View File

@@ -7,7 +7,7 @@ import java.util.stream.Stream;
public class Main {
private static long test(Map<String, List<String>> strings) {
long sum = 0;
long sum = 0L;
for (Map.Entry<String, List<String>> e : strings.entrySet()) {
if (!e.getKey().isEmpty()) {
long count = e.getValue().stream().filter(new Predicate<String>() {

View File

@@ -6,7 +6,7 @@ import java.util.stream.Stream;
public class Main {
private static long test(Map<String, List<String>> strings) {
long sum = 0;
long sum = 0L;
for (Map.Entry<String, List<String>> e : strings.entrySet()) {
if (!e.getKey().isEmpty()) {
long count = e.getValue().stream().filter(s -> e.getKey().equals(s)).count();

View File

@@ -6,7 +6,7 @@ import java.util.stream.Stream;
public class Main {
private static long test(Map<String, List<String>> strings) {
long sum = 0;
long sum = 0L;
for (Map.Entry<String, List<String>> s : strings.entrySet()) {
if (!s.getKey().isEmpty()) {
long count = s.getValue().stream().filter(sx -> s.getKey().equals(sx)).count();

View File

@@ -7,7 +7,7 @@ import static java.util.Arrays.asList;
public class Main {
public static long test(List<String> list) {
long count = 0;
long count = 0L;
for (String l : list) {
if (l != null) {
(new Consumer<String>() {

View File

@@ -7,7 +7,7 @@ import static java.util.Arrays.asList;
public class Main {
public static long test(List<String> list) {
long count = 0;
long count = 0L;
for (String l : list) {
if (l != null) {
(new Consumer<String>() {

View File

@@ -0,0 +1,17 @@
// "Replace Stream API chain with loop" "true"
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Boolean, Long> map2 = new HashMap<>();
map2.put(false, 0L);
map2.put(true, 0L);
for (String s : list) {
map2.merge(s.isEmpty(), 1L, Long::sum);
}
}
}

View File

@@ -0,0 +1,17 @@
// "Replace Stream API chain with loop" "true"
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Boolean, Double> map1 = new HashMap<>();
map1.put(false, 0.0);
map1.put(true, 0.0);
for (String s : list) {
map1.merge(s.isEmpty(), (double) s.length(), Double::sum);
}
}
}

View File

@@ -4,7 +4,7 @@ import java.util.stream.IntStream;
public class Main {
private static long check(int start, int stop, double v) {
long count = 0;
long count = 0L;
for (int x = start; x < stop; x++) {
double x1 = 1.0 / x;
if (x1 < v) {

View File

@@ -4,7 +4,7 @@ import java.util.stream.IntStream;
public class Main {
private static long check(int start, double val) {
long count = 0;
long count = 0L;
int bound = start * 200;
for (int x = start; x <= bound; x++) {
double v = 1.0 / x;

View File

@@ -6,7 +6,7 @@ import java.util.function.LongSupplier;
public class Main {
private static void test(List<String> list) {
// and filter!
long count1 = 0;
long count1 = 0L;
for (String s : list) {
if (!s/* comment */.isEmpty()) {
count1++;

View File

@@ -4,7 +4,7 @@ import java.util.*;
public class Main {
private static long test(List<?> list) {
long count = 0;
long count = 0L;
Set<Object> uniqueValues = new HashSet<>();
long toSkip = list.size() / 2;
for (Object o : list) {

View File

@@ -0,0 +1,11 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Integer, Long> map = list.stream().coll<caret>ect(Collectors.groupingBy(String::length, Collectors.counting()));
}
}

View File

@@ -0,0 +1,11 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Integer, Integer> map3 = list.stream().coll<caret>ect(Collectors.groupingBy(String::length, Collectors.mapping(String::trim, Collectors.summingInt(String::length))));
}
}

View File

@@ -0,0 +1,11 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Integer, Double> map4 = list.stream().co<caret>llect(Collectors.groupingBy(String::length, Collectors.summingDouble(String::length)));
}
}

View File

@@ -0,0 +1,12 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Boolean, Long> map2 = list.stream()
.co<caret>llect(Collectors.partitioningBy(String::isEmpty, Collectors.counting()));
}
}

View File

@@ -0,0 +1,12 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
static void test(List<String> list) {
Map<Boolean, Double> map1 = list.stream()
.col<caret>lect(Collectors.partitioningBy(String::isEmpty, Collectors.summingDouble(String::length)));
}
}