不兼容的通用通配符捕获

发布于 2024-10-08 02:09:32 字数 997 浏览 9 评论 0原文

在以下代码片段中:

package test;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class WildcardsTest<K, V> {
    private Iterator<Map.Entry<K, ? extends Collection<V>>> iterator;
    public WildcardsTest(Map<K, ? extends Collection<V>> map) {
        iterator = map.entrySet().iterator();
        /* Type mismatch: cannot convert from
           Iterator<Map.Entry<K,capture#1-of ? extends Collection<V>>> to
           Iterator<Map.Entry<K,? extends Collection<V>>> */
    }
}

尽管类型似乎完全匹配,但分配不正确。

我设计了一个肮脏的解决方法,将 Collection 的类型指定为另一个泛型参数,如下所示

public class WildcardsTest<K, V, C extends Collection<V>> {
    private Iterator<Map.Entry<K, C>> iterator;
    public WildcardsTest(Map<K, C> map) {
        iterator = map.entrySet().iterator();
    }
}

:有什么方法可以在保持类型安全的同时摆脱它吗?

谢谢。

in the following snippet:

package test;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class WildcardsTest<K, V> {
    private Iterator<Map.Entry<K, ? extends Collection<V>>> iterator;
    public WildcardsTest(Map<K, ? extends Collection<V>> map) {
        iterator = map.entrySet().iterator();
        /* Type mismatch: cannot convert from
           Iterator<Map.Entry<K,capture#1-of ? extends Collection<V>>> to
           Iterator<Map.Entry<K,? extends Collection<V>>> */
    }
}

The assignment is incorrect, although the types seem to match exactly.

I've devised a dirty workaround by specifying the type of the Collection as another generic parameter, like this:

public class WildcardsTest<K, V, C extends Collection<V>> {
    private Iterator<Map.Entry<K, C>> iterator;
    public WildcardsTest(Map<K, C> map) {
        iterator = map.entrySet().iterator();
    }
}

But that C parameter is really a "don't care" type that only complicates the API, is there any way to get rid of it while maintaining type safety?

Thanks.

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

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

发布评论

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

评论(2

青瓷清茶倾城歌 2024-10-15 02:09:32

这样做,它会工作:

private final Iterator<? extends
    Map.Entry<K, ? extends Collection<V>>
> iterator;

您仍然可以像这样使用迭代器:

public void foo(){
    while(iterator.hasNext()){
        Entry<K, ? extends Collection<V>> entry = iterator.next();
        Collection<V> value = entry.getValue();
    }
}

作为参考,请阅读 get 和 put 原则(最初来自 Java 泛型和集合

Do it like this and it will work:

private final Iterator<? extends
    Map.Entry<K, ? extends Collection<V>>
> iterator;

You can still use the iterator like this:

public void foo(){
    while(iterator.hasNext()){
        Entry<K, ? extends Collection<V>> entry = iterator.next();
        Collection<V> value = entry.getValue();
    }
}

For reference, read the get and put principle (originally from Java Generics and Collections)

长亭外,古道边 2024-10-15 02:09:32

尽管类型看起来完全匹配,但分配不正确。

两个 ? 通配符可以绑定到两个不同的类。这样说,很明显存在类型不匹配:

private Iterator<Map.Entry<K, ArrayList<V>>> iterator;
public WildcardsTest(Map<K, HashSet<V>> map) {
    iterator = map.entrySet().iterator();
}

当您引入C时,您“强迫它们”引用同一个类。

The assignment is incorrect, although the types seem to match exactly.

The two ?-wildcards can be bound to two different classes. Put it this way, it is quite obvious that there is a type mismatch:

private Iterator<Map.Entry<K, ArrayList<V>>> iterator;
public WildcardsTest(Map<K, HashSet<V>> map) {
    iterator = map.entrySet().iterator();
}

When you introduce the C, you "force them" to refer to the same class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文