mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-160448 Simplify groupingBy-collectingAndThen-Optional.get Collectors chain
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
// "Use 'Collectors.toConcurrentMap' collector" "true"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
List<String> input = Arrays.asList("a", "bbb", "cc", "ddd", " x", "ee");
|
||||
Map<Integer, String> map3 = input.stream().collect(Collectors.toConcurrentMap(String::length, Function.identity(), BinaryOperator.maxBy(String::compareTo)));
|
||||
System.out.println(map3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// "Use 'Collectors.toMap' collector" "true"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
List<String> input = Arrays.asList("a", "bbb", "cc", "ddd", " x", "ee");
|
||||
// group by length
|
||||
// <= reduce to max
|
||||
/*need to unwrap optional*/
|
||||
Map<Integer, String> map2 = input.stream().collect(
|
||||
Collectors.toMap(String::length, Function.identity(), (s1, s2) -> s1.compareTo(s2) > 0 ? /*find max*/ s1 : s2));
|
||||
System.out.println(map2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Use 'Collectors.toMap' collector" "true"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
List<String> input = Arrays.asList("a", "bbb", "cc", "ddd", " x", "ee");
|
||||
Map<Integer, String> map = input.stream().collect(Collectors.toMap(String::length, String::trim, (s1, s2) -> s1.compareTo(s2) > 0 ? s1 : s2));
|
||||
System.out.println(map);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Use 'Collectors.toConcurrentMap' collector" "true"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
List<String> input = Arrays.asList("a", "bbb", "cc", "ddd", " x", "ee");
|
||||
Map<Integer, String> map3 = input.stream().collect(Collectors.groupingB<caret>yConcurrent(String::length, Collectors
|
||||
.collectingAndThen(Collectors.maxBy(String::compareTo), Optional::get)));
|
||||
System.out.println(map3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// "Use 'Collectors.toMap' collector" "true"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
List<String> input = Arrays.asList("a", "bbb", "cc", "ddd", " x", "ee");
|
||||
Map<Integer, String> map2 = input.stream().collect(
|
||||
Collectors.gr<caret>oupingBy(String::length, // group by length
|
||||
Collectors.collectingAndThen(
|
||||
Collectors.reducing((s1, s2) -> s1.compareTo(s2) > 0 ? /*find max*/ s1 : s2), // <= reduce to max
|
||||
/*need to unwrap optional*/ s -> s.get())));
|
||||
System.out.println(map2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// "Use 'Collectors.toMap' collector" "true"
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
List<String> input = Arrays.asList("a", "bbb", "cc", "ddd", " x", "ee");
|
||||
Map<Integer, String> map = input.stream().collect(Collectors
|
||||
.gr<caret>oupingBy(String::length, Collectors.mapping(String::trim, Collectors
|
||||
.collectingAndThen(Collectors
|
||||
.reducing((s1, s2) -> s1.compareTo(s2) > 0 ? s1 : s2), Optional::get))));
|
||||
System.out.println(map);
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Tagir Valeev
|
||||
*/
|
||||
public class SimplifyCollectorInspectionTest extends LightQuickFixParameterizedTestCase {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[]{new SimplifyCollectorInspection()};
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
doAllTests();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/inspection/simplifyCollector";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user