Java Interface Interface addAll ()

What difference does it make if the Java Collection Interface has an addAll method signature like this

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

and not boolean addAll(Collection<? extends E> c);?

thank

-Abidi

+3
source share
6 answers

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 is no difference in the byte code received (except for the generated debug code). Therefore, if the two versions are equivalent, you can also stick to a version that is easier to understand.

+2
source

In this case, the presence of <?>or <T>equivalent to users addAll.

, , <T> addAll.

+4

, T in <T extends E> boolean addAll(Collection<T> c) , addAll , E . , , , E, Collection<? extends E>.

.

+2

T, , .

0

, T - , ?.

0
source

I do not think this compiled. A method cannot have two return types ( <T>and boolean) at the same time.

-2
source

Source: https://habr.com/ru/post/1792279/


All Articles