`
leonzhx
  • 浏览: 767651 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 28: Use bounded wildcards to increase API flexibility

阅读更多

1.  While it is counterintuitive that List<String> is not a subtype of List<Object>, it really does make sense. You can put any object into a List<Object>, but you can put only strings into a List<String>.

 

2.  In the following codes, instead of using Iterable<E> , you’d better use Iterable<? extends E>:

public class Stack<E> {
    public Stack();
    public void push(E e);
    public E pop();
    public boolean isEmpty();
    // pushAll method without wildcard type - deficient!
    public void pushAll(Iterable<E> src) {
        for (E e : src)
            push(e);
    }
}

In this way, you are able to pass Iterable<Integer> to Stack<Number> while otherwise you can’t.

 

3.  In the following codes, instead of using Collection <E> , you’d better use Collection <? super E>:

public class Stack<E> {
    public Stack();
    public void push(E e);
    public E pop();
    public boolean isEmpty();
    // Wildcard type for parameter that serves as an E consumer
    public void popAll(Collection<E> dst) {
        while (!isEmpty())
            dst.add(pop());
    }
}

 

In this way, you are able to put pass Collection<Number> to Stack<Integer> while otherwise you can’t.

 

4.  For maximum flexibility, use wildcard types on input parameters that represent producers or consumers. If an input parameter is both a producer and a consumer, then wildcard types will do you no good: you need an exact type match, which is what you get without any wildcards.

 

5.  Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code.

 

6.  Comparables are always consumers, you should always use Comparable<? super T> in preference to Comparable<T>. The same is true of comparators, you should always use Comparator<? super T> in preference to Comparator<T>. In the following codes:

public static <T extends Comparable<? super T>> T max(List<? extends T> list)

List<ScheduledFuture<?>> scheduledFutures can be passed to the above method while otherwise can’t, ScheduledFuture does not implement Comparable<ScheduledFuture>. Instead, it is a subinterface of Delayed, which extends Comparable<Delayed>.

 

7.  If a type parameter appears only once in a method declaration, replace it with a wildcard. If it’s an unbounded type parameter, replace it with an unbounded wildcard; if it’s a bounded type parameter, replace it with a bounded wildcard.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics