Java 集合接口 addAll() 方法签名

发布于 2024-10-17 07:05:02 字数 238 浏览 2 评论 0原文

如果 Java Collection Interface 有像这样的 addAll 方法签名

 <T extends E> boolean addAll(Collection<T> c);

而不是 boolean addAll(Collection c);?

谢谢

-阿比迪

What difference would it make if Java Collection Interface has addAll method signature like this

 <T extends E> boolean addAll(Collection<T> c);

rather than
boolean addAll(Collection<? extends E> c);?

Thanks

-Abidi

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

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

发布评论

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

评论(6

清醇 2024-10-24 07:05:02

在这种情况下,使用 对于 addAll 的用户来说是等效的。

我认为使用前一种符号是为了清楚起见,因为使用 会使 addAll 的签名更加复杂。

In this case, having <?> or <T> is equivalent for users of addAll.

I think that the former notation was used for clarity, because using <T> makes the signature of addAll more complicated.

云仙小弟 2024-10-24 07:05:02

以这个测试界面为例:

public interface DumbTestInterface<E> {

    <T extends E> boolean addAll1(Collection<T> c);

    boolean addAll2(Collection<? extends E> c);

}

这是字节代码:

// Compiled from DumbTestInterface.java (version 1.6 : 50.0, no super bit)
// Signature: <E:Ljava/lang/Object;>Ljava/lang/Object;
public abstract interface rumba.dumba.DumbTestInterface {

  // Method descriptor #6 (Ljava/util/Collection;)Z
  // Signature: <T:TE;>(Ljava/util/Collection<TT;>;)Z
  public abstract boolean addAll1(java.util.Collection arg0);

  // Method descriptor #6 (Ljava/util/Collection;)Z
  // Signature: (Ljava/util/Collection<+TE;>;)Z
  public abstract boolean addAll2(java.util.Collection arg0);
}

正如您所看到的,生成的字节代码没有任何区别(除了生成的调试代码)。因此,如果两个版本是等效的,您不妨坚持使用更容易理解的版本。

Take this test interface:

public interface DumbTestInterface<E> {

    <T extends E> boolean addAll1(Collection<T> c);

    boolean addAll2(Collection<? extends E> c);

}

Here's the byte code:

// Compiled from DumbTestInterface.java (version 1.6 : 50.0, no super bit)
// Signature: <E:Ljava/lang/Object;>Ljava/lang/Object;
public abstract interface rumba.dumba.DumbTestInterface {

  // Method descriptor #6 (Ljava/util/Collection;)Z
  // Signature: <T:TE;>(Ljava/util/Collection<TT;>;)Z
  public abstract boolean addAll1(java.util.Collection arg0);

  // Method descriptor #6 (Ljava/util/Collection;)Z
  // Signature: (Ljava/util/Collection<+TE;>;)Z
  public abstract boolean addAll2(java.util.Collection arg0);
}

As you can see there's no difference in the resulting byte code (apart from the generated debug code). So if the two versions are equivalent, you might as well stick with the version that's easier to understand.

深海里的那抹蓝 2024-10-24 07:05:02

问题是,中的 T 是这样的: boolean addAll(Collectionc) 完全没有必要,因为 addAll 不关心 E 的具体子类型它给出的集合包含。它所关心的是它给定的集合包含E某些子类型,这正是Collection 的意思。

您不应该向方法引入不必要的泛型类型。

The thing is, the T in <T extends E> boolean addAll(Collection<T> c) is completely unnecessary, because addAll doesn't care what specific subtype of E the collection it's given contains. All it cares is that the collection it's given contains some subtype of E, which is exactly what Collection<? extends E> means.

You shouldn't introduce unnecessary generic types to a method.

救赎№ 2024-10-24 07:05:02

如果您使用显式类 T,则无法将通配符集合传递给该方法,而您可能想要并且应该能够这样做。

If you used an explicit class T, then you could not pass a wildcarded collection to the method, which is something that you may want to do and should be able to.

楠木可依 2024-10-24 07:05:02

基本上,当类型变量 T 仅在参数类型中的某个位置使用时,您可以安全地将其更改为 ?

Basically, when the type variable T is only going to be used in one place somewhere in the types of the parameters, you can safely change it to ?.

北城孤痞 2024-10-24 07:05:02

我认为这不会编译。一个方法不能同时有两种返回类型(boolean)。

I don't think that would compile. A method cannot have two return types (<T> and boolean) at the same time.

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