When to use generic methods and when to use wild-card?

前端 未结 9 2294
猫巷女王i
猫巷女王i 2020-11-22 10:32

I am reading about generic methods from OracleDocGenericMethod. I am pretty confused about the comparison when it says when to use wild-card and when to use generic methods.

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 10:44

    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.

    For example:

    public  T giveMeMaximum(Collection items);
    public  Collection applyFilter(Collection items);
    

    Here you are extracting some of the T following a certain criteria. If T is Long your methods will return Long and Collection; the actual return type is dependent on the parameter type, thus it is useful, and advised, to use generic types.

    When this is not the case you can use wild card types:

    public int count(Collection items);
    public boolean containsDuplicate(Collection items);
    

    In this two example whatever the type of the items in the collections the return types will be int and boolean.

    In your examples:

    interface Collection {
        public boolean containsAll(Collection c);
        public boolean addAll(Collection c);
    }
    

    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.

    Second question:

    class Collections {
        public static  void copy(List dest, List src) {
        ...
    }
    

    This first code allow you to pass an heterogeneous List src as a parameter. This list can contain multiple elements of different classes as long as they all extends the base class T.

    if you had:

    interface Fruit{}
    

    and

    class Apple implements Fruit{}
    class Pear implements Fruit{}
    class Tomato implements Fruit{}
    

    you could do

    List basket = new ArrayList();
    basket.add(new Apple());
    basket.add(new Pear());
    basket.add(new Tomato());
    List fridge = new ArrayList(); 
    
    Collections.copy(fridge, basket);// works 
    

    On the other hand

    class Collections {
        public static  void copy(List dest, List src) {
        ...
    }
    

    constrain List src 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:

    List basket = new ArrayList();
    basket.add(new Apple());
    basket.add(new Apple());
    basket.add(new Apple());
    List fridge = new ArrayList();
    
    Collections.copy(fridge, basket); /* works since the basket is defined as a List of apples and not a list of some fruits. */
    

提交回复
热议问题