mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 03:13:40 +07:00
StreamToLoop: reuse variable if possible when the stream is used in the declaration
This commit is contained in:
+6
-5
@@ -6,13 +6,14 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Boolean, Map<Character, Set<String>>> map = new HashMap<>();
|
||||
map.put(false, new HashMap<>());
|
||||
map.put(true, new HashMap<>());
|
||||
final Map<Boolean, Map<Character, Set<String>>> nestedMap =
|
||||
new HashMap<>();
|
||||
nestedMap.put(false, new HashMap<>());
|
||||
nestedMap.put(true, new HashMap<>());
|
||||
for (String s : strings) {
|
||||
map.get(s.length() > 2).computeIfAbsent(s.charAt(0), k -> new HashSet<>()).add(s);
|
||||
nestedMap.get(s.length() > 2).computeIfAbsent(s.charAt(0), k -> new HashSet<>()).add(s);
|
||||
}
|
||||
System.out.println(map);
|
||||
System.out.println(nestedMap);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+3
-3
@@ -5,13 +5,13 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, String> test(List<String> strings) {
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
Map<Integer, String> mapping = new HashMap<>();
|
||||
for (String str : strings) {
|
||||
if (map.put(str.length(), str) != null) {
|
||||
if (mapping.put(str.length(), str) != null) {
|
||||
throw new IllegalStateException("Duplicate key");
|
||||
}
|
||||
}
|
||||
return map;
|
||||
return mapping;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+3
-1
@@ -6,7 +6,9 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().c<caret>ollect(Collectors.partitioningBy((String s) -> s.length() > 2, Collectors.groupingBy(s -> s.charAt(0), Collectors.toSet()))));
|
||||
final Map<Boolean, Map<Character, Set<String>>> nestedMap =
|
||||
strings.stream().c<caret>ollect(Collectors.partitioningBy((String s) -> s.length() > 2, Collectors.groupingBy(s -> s.charAt(0), Collectors.toSet())));
|
||||
System.out.println(nestedMap);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+2
-1
@@ -5,8 +5,9 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, String> test(List<String> strings) {
|
||||
return strings.stream()
|
||||
Map<Integer, String> mapping = strings.stream()
|
||||
.co<caret>llect(Collectors.toMap(String::length, str -> str));
|
||||
return mapping;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user