类型推断问题:"incompatible types: cannot infer type-variable(s) T,K,U" for map inside stream

Type inference problem: "incompatible types: cannot infer type-variable(s) T,K,U" for map inside stream

我对下面的代码有疑问(不要注意它的意义,简化只是为了显示错误):

package net.igorok;

import java.util.*;

public class Main {
    public static void main(String[] args) {

        EntryRecord<Integer, String> data_1 = new EntryRecord(0, "Xiaomi");
        EntryRecord<Integer, String> data_2 = new EntryRecord(1, "Apple");

        List<EntryRecord<Integer, String>> data = new ArrayList<>();
        data.add(data_1);
        data.add(data_2);

        Operation operation = new Operation();
        operation.transform(data);
    }
}

.

package net.igorok;

public class EntryRecord<K,V> {
    private K key;
    private V value;

    public EntryRecord(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }
    public V getValue() {
        return value;
    }

    //public void setKey(K key), setValue(V value), equals(), hashcode()...
}

.

package net.igorok;

import java.util.Collection;

public interface OperationContract<V, R> {
    Collection<R> transform(Collection<V> collection);
}

.

package net.igorok;

import java.util.*;
import java.util.stream.Collectors;

public class Operation<V, R> implements OperationContract<V, R> {

    @Override
    public Collection<R> transform(Collection<V> collection) {
        Map<Integer, String> map = (Map<Integer, String>)  collection.stream()
                .collect(Collectors.toMap(EntryRecord::getKey, EntryRecord::getValue));

        // Do not pay attention to return, it is just for example
        return new ArrayList();
    }
}

在这个class中,“EntryRecord::getKey”和“EntryRecord::getValue”用红色标记(“非静态方法不能从静态上下文中引用”,但是因为我明白,这是一个 IntelliJ IDEA 错误)。

我在尝试编译时收到的消息是: .

/home/punisher/Dropbox/IdeaProjects/ToSOF/src/net/igorok/Operation.java:11:42 java: incompatible types: cannot infer type-variable(s) T,K,U
    (argument mismatch; invalid method reference
      method getKey in class net.igorok.EntryRecord<K,V> cannot be applied to given types
        required: no arguments
        found:    java.lang.Object
        reason: actual and formal argument lists differ in length)

.

我已经阅读过一些有类似问题的类似帖子,但我不明白我需要在代码中更改什么。我明白,这是因为类型推断,但我不擅长泛型和类型推断。

你能告诉我,我可以在代码中更改什么以使其工作吗?为什么?

谢谢!

public class Operation<V, R> implements OperationContract<V, R> {

    @Override
    public Collection<R> transform(Collection<V> collection) {
        Map<Integer, String> map = (Map<Integer, String>) collection.stream()
           .map(e -> (EntryRecord<Integer, String>)e)
                .collect(Collectors.toMap(EntryRecord::getKey, EntryRecord::getValue));

        // Do not pay attention to return, it is just for example
        return new ArrayList();
    }
}

适合你的情况。但是,总的来说,这不是一个“好”代码。