Java 不兼容类型 无法转换为通配符参数 HashMap<> 里面

最近,我在编译过程中遇到了以下错误:

error: incompatible types: HashMap<Integer, HashMap<Object, Consumer<A>>> cannot be converted
to HashMap<Integer, HashMap<Object, Consumer<? extends B>>>

在此代码中,类 a b 的直接子类。

此错误发生在以下片段中:

HashMap<Integer, HashMap<Object, Consumer<A>>> item = new HashMap<>();
HashSet<HashMap<Integer, HashMap<Object, Consumer<? extends B>>>> set = new HashSet();
set.add(item);

试图解决错误,我已经尝试删除?从 hashmap 扩展位 hashset 的内部,但是编译器不断丢弃错误。

Recently I have come across the following error during compilation:

error: incompatible types: HashMap<Integer, HashMap<Object, Consumer<A>>> cannot be converted
to HashMap<Integer, HashMap<Object, Consumer<? extends B>>>

In this code, class A is a direct subclass of B.

This error occurred in the following snippet:

HashMap<Integer, HashMap<Object, Consumer<A>>> item = new HashMap<>();
HashSet<HashMap<Integer, HashMap<Object, Consumer<? extends B>>>> set = new HashSet();
set.add(item);

In attempting to resolve the error I have already tried removing the ? extends bit from the HashMap inside of the HashSet, but the compiler kept throwing the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2025-01-26 14:49:40

消费者&lt; a&gt; 消费者&lt;扩展B&gt; 是完全不同的,实际上消费者?扩展b&gt; 可以是消费者&lt; c&gt; 其中 c 是另一个类的类,

consemer&lt; a&gt; 相同。 消费者&lt; b&gt; 它们不是同一回事, b 类的实例不是 a 类的实例,因此请删除<<<<<代码>?扩展仍然会给您编译时间错误。

import java.util.ArrayList;
import java.util.List;

public class Test {
    static class Lion extends Animal {
    }
    static class Animal {
    }
    public static void main(String[] args) {
        List<? extends Animal> animals = new ArrayList<Animal>();
        animals .add(new Lion()); // DOES NOT COMPILE 

        // at compile time , our List(reference) may be a List of Animal or List of Lion , not a List of both of them
        
        // using ? extends Animal to refer the ArrayList instance 
        // we cannot add a Lion to an ArrayList of Animal

        animals .add(new Animal()); // DOES NOT COMPILE

        // the same using the ? extends Animal as reference 
       // we cannot add an animal to an ArrayList of Lion
    }
}

Consumer<A> and Consumer<? extends B> are totally different , in fact Consumer<? extends B> can be Consumer<C> where C is another class who extends B.

The same with Consumer<A> and Consumer<B> they are not the same thing, an instance of B class is not an instance of A class, so deleting the ? extends still give you a compile time error.

import java.util.ArrayList;
import java.util.List;

public class Test {
    static class Lion extends Animal {
    }
    static class Animal {
    }
    public static void main(String[] args) {
        List<? extends Animal> animals = new ArrayList<Animal>();
        animals .add(new Lion()); // DOES NOT COMPILE 

        // at compile time , our List(reference) may be a List of Animal or List of Lion , not a List of both of them
        
        // using ? extends Animal to refer the ArrayList instance 
        // we cannot add a Lion to an ArrayList of Animal

        animals .add(new Animal()); // DOES NOT COMPILE

        // the same using the ? extends Animal as reference 
       // we cannot add an animal to an ArrayList of Lion
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文