Edit :: Jestem Parafrazując pytanie więc, że jest to bardziej oczywisteJava 8 Streams - Grupowanie strumień krotki
Pisałem ten kod
List<ImmutablePair<Integer, Integer>> list = new ArrayList<ImmutablePair<Integer, Integer>>();
list.add(new ImmutablePair(1, 1));
list.add(new ImmutablePair(1, 1));
list.add(new ImmutablePair(1, 1));
list.add(new ImmutablePair(2, 2));
list.add(new ImmutablePair(2, 2));
list.add(new ImmutablePair(2, 2));
list.add(new ImmutablePair(3, 3));
list.add(new ImmutablePair(3, 3));
list.add(new ImmutablePair(3, 3));
Stream<ImmutablePair<Integer, Integer>> stream = list.stream();
Map<Integer, Integer> result = stream.collect(Collectors.groupingBy(
ImmutablePair::getLeft,
Collectors.mapping(
ImmutablePair::getRight,
Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
)
));
Chcę przetworzyć tę listę za pomocą paruje tak że wyjście jest mapą zawierającą klucze (1, 3), (2, 6), (3, 9)
Tak więc, po prostu grupujemy lewą pozycję krotki, a następnie sumujemy właściwą pozycję.
Ten kod nie jest kompilowany, a kompilator mówi, że nie może rozwiązać metod getLeft, getRight.
Oto komunikaty o błędach z kompilatora
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:229: error: method summingInt in class Collectors cannot be applied to given types;
Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
^
required: ToIntFunction<? super T#1>
found: Comparator<Object>
reason: no instance(s) of type variable(s) T#2,U exist so that Comparator<T#2> conforms to ToIntFunction<? super T#1>
where T#1,T#2,U are type-variables:
T#1 extends Object declared in method <T#1>summingInt(ToIntFunction<? super T#1>)
T#2 extends Object declared in method <T#2,U>comparing(Function<? super T#2,? extends U>)
U extends Comparable<? super U> declared in method <T#2,U>comparing(Function<? super T#2,? extends U>)
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:229: error: incompatible types: cannot infer type-variable(s) T,U
Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
^
(argument mismatch; invalid method reference
no suitable method found for getRight(Object)
method Pair.getRight() is not applicable
(actual and formal argument lists differ in length)
method ImmutablePair.getRight() is not applicable
(actual and formal argument lists differ in length))
where T,U are type-variables:
T extends Object declared in method <T,U>comparing(Function<? super T,? extends U>)
U extends Comparable<? super U> declared in method <T,U>comparing(Function<? super T,? extends U>)
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:229: error: invalid method reference
Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
^
non-static method getRight() cannot be referenced from a static context
where R is a type-variable:
R extends Object declared in class ImmutablePair
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:228: error: invalid method reference
ImmutablePair::getRight,
^
non-static method getRight() cannot be referenced from a static context
where R is a type-variable:
R extends Object declared in class ImmutablePair
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:226: error: invalid method reference
ImmutablePair::getLeft,
^
non-static method getLeft() cannot be referenced from a static context
where L is a type-variable:
L extends Object declared in class ImmutablePair
Powinieneś zamieścić prosty przykład demonstrujący problem, w przeciwnym razie trudno nam go odtworzyć. – Tunaki
Co z kolorem Collectors.groupBy (ImmutablePair :: getLeft)? Mam na myśli osobno. Czy to także się nie udaje? –
Próbujesz grupować instancje ImmutablePair, odwołując się do metody Movie. To nie może działać. To jak sortowanie osób według koloru banana. Możesz sortować ludzi według ich wzrostu, wagi lub wieku, ale nie przez własność banana. Możemy wyjaśnić, co dokładnie oznacza komunikat o błędzie, jeśli wysłałeś dokładny komunikat o błędzie zamiast parafrazować go i podajesz własne, błędne zrozumienie wiadomości. Możemy pomóc Ci naprawić kod, jeśli opublikujesz zaangażowane zajęcia. –