Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your first question: It means that if there is a relation between the parameter's type and the method's return type then use a generic.</p> <p>For example:</p> <pre><code>public &lt;T&gt; T giveMeMaximum(Collection&lt;T&gt; items); public &lt;T&gt; Collection&lt;T&gt; applyFilter(Collection&lt;T&gt; items); </code></pre> <p>Here you are extracting some of the T following a certain criteria. If T is <code>Long</code> your methods will return <code>Long</code> and <code>Collection&lt;Long&gt;</code>; the actual return type is dependent on the parameter type, thus it is useful, and advised, to use generic types.</p> <p>When this is not the case you can use wild card types:</p> <pre><code>public int count(Collection&lt;?&gt; items); public boolean containsDuplicate(Collection&lt;?&gt; items); </code></pre> <p>In this two example whatever the type of the items in the collections the return types will be <code>int</code> and <code>boolean</code>.</p> <p>In your examples:</p> <pre><code>interface Collection&lt;E&gt; { public boolean containsAll(Collection&lt;?&gt; c); public boolean addAll(Collection&lt;? extends E&gt; c); } </code></pre> <p>those two functions will return a boolean whatever is the types of the items in the collections. In the second case it is limited to instances of a subclass of E.</p> <p>Second question:</p> <pre><code>class Collections { public static &lt;T&gt; void copy(List&lt;T&gt; dest, List&lt;? extends T&gt; src) { ... } </code></pre> <p>This first code allow you to pass an heterogeneous <code>List&lt;? extends T&gt; src</code> as a parameter. This list can contain multiple elements of different classes as long as they all extends the base class T.</p> <p>if you had:</p> <pre><code>interface Fruit{} </code></pre> <p>and</p> <pre><code>class Apple implements Fruit{} class Pear implements Fruit{} class Tomato implements Fruit{} </code></pre> <p>you could do</p> <pre><code>List&lt;? extends Fruit&gt; basket = new ArrayList&lt;? extends Fruit&gt;(); basket.add(new Apple()); basket.add(new Pear()); basket.add(new Tomato()); List&lt;Fruit&gt; fridge = new ArrayList&lt;Fruit&gt;(); Collections.copy(fridge, basket);// works </code></pre> <p>On the other hand</p> <pre><code>class Collections { public static &lt;T, S extends T&gt; void copy(List&lt;T&gt; dest, List&lt;S&gt; src) { ... } </code></pre> <p>constrain <code>List&lt;S&gt; src</code> to be of one particular class S that is a subclass of T. The list can only contain elements of one class (in this instance S) and no other class, even if they implement T too. You wouldn't be able to use my previous example but you could do:</p> <pre><code>List&lt;Apple&gt; basket = new ArrayList&lt;Apple&gt;(); basket.add(new Apple()); basket.add(new Apple()); basket.add(new Apple()); List&lt;Fruit&gt; fridge = new ArrayList&lt;Fruit&gt;(); Collections.copy(fridge, basket); /* works since the basket is defined as a List of apples and not a list of some fruits. */ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload