类型推断问题:“不兼容的类型:无法推断流内映射的类型变量 T、K、U”

伊戈尔

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

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();
    }
}

在这个类中,“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();
    }
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

内联代码-不兼容的类型:无法推断类型变量T

无法推断嵌套类型的类型变量T

无法推断“ U”的类型

不兼容类型:推断变量T具有不兼容范围

java:不兼容类型:推断变量T具有不兼容边界等式约束:下界:java.util.List <>

不兼容的类型:推断变量T具有不兼容的边界相等约束:捕获的#1?扩展java.lang.Object

Scala类型推断:无法从Array [T]推断IndexedSeq [T]

类型不匹配:推断的类型为T但kotlin。

LinkedList的“不兼容类型:无法推断类型变量”

返回通用类型时*我收到错误信息,无法推断类型变量T

如果类型T和U相同,则功能模板无法推断类型

Rust 泛型 - 无法推断类型参数“T”的类型

Java Optional.map() 错误:“无法推断 <U> map(Function<? super T,? extends U>) 的类型参数”

从返回类型推断类型T的模板

没有足够的信息来推断类型变量T

结果类型无法快速推断通用参数“ T” 5

当在结构定义中明确指定类型时,无法推断类型参数T的类型

错误Kotlin:类型不匹配:推断类型为T?但没想到

Java 8 [无法推断类型变量]问题

如何解决所需的类型注释无法推断类型参数“T”的类型?编译这段代码需要什么类型的注解?

该方法如何推断<T>的类型

为什么在通用函数类型参数推断中,Scala无法将Null类型识别为T的子类型?

推断变量T具有不兼容的范围

类型推断-无法推断Monad

没有足够的信息来推断类型变量 T1

java.io.BufferedReader()。map无法从<Stream>推断<T>的类型参数(Stream <?扩展T>)

java不兼容类型T无法转换为Enum

Java 不兼容类型:T 无法转换为 My Class

错误无法推断类型参数“ T”。使用Map.fromIterable时