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

前端 未结 9 2192
猫巷女王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:55

    Wildcard method is also generic - you could call it with some range of types.

    The <T> syntax defines a type variable name. If a type variable has any use (e.g. in method implementation or as a constraint for other type), then it makes sense to name it, otherwise you could use ?, as anonymous variable. So, looks like just a short-cut.

    Moreover, the ? syntax is not avoidable when you declare a field:

    class NumberContainer
    {
     Set<? extends Number> numbers;
    }
    
    0 讨论(0)
  • 2020-11-22 10:59

    Consider following example from The Java Programming by James Gosling 4th edition below where we want to merge 2 SinglyLinkQueue:

    public static <T1, T2 extends T1> void merge(SinglyLinkQueue<T1> d, SinglyLinkQueue<T2> s){
        // merge s element into d
    }
    
    public static <T> void merge(SinglyLinkQueue<T> d, SinglyLinkQueue<? extends T> s){
            // merge s element into d
    }
    

    Both of the above methods have the same functionality. So which is preferable? Answer is 2nd one. In the author's own words :

    "The general rule is to use wildcards when you can because code with wildcards is generally more readable than code with multiple type parameters. When deciding if you need a type variable, ask yourself if that type variable is used to relate two or more parameters, or to relate a parameter type with the return type. If the answer is no, then a wildcard should suffice."

    Note: In book only second method is given and type parameter name is S instead of 'T'. First method is not there in the book.

    0 讨论(0)
  • 2020-11-22 11:07

    I will try and answer your question, one by one.

    Don't we think wild card like (Collection<? extends E> c); is also supporting kind of polymorphism?

    No. The reason is that the bounded wildcard has no defined parameter type. It is an unknown. All it "knows" is that the "containment" is of a type E (whatever defined). So, it cannot verify and justify whether the value provided matches the bounded type.

    So, it's no sensible to have polymorphic behaviours on wildcards.

    The document discourages the second declaration and promotes usage of first syntax? What's the difference between the first and second declaration? Both seems to be doing the same thing?

    The first option is better in this case as T is always bounded, and source will definitely have values (of unknowns) that subclasses T.

    So, suppose that you want to copy all list of numbers, the first option will be

    Collections.copy(List<Number> dest, List<? extends Number> src);
    

    src, essentially, can accept List<Double>, List<Float>, etc. as there is an upper bound to the parameterized type found in dest.

    The 2nd option will force you to bind S for every type you want to copy, like so

    //For double 
    Collections.copy(List<Number> dest, List<Double> src); //Double extends Number.
    
    //For int
    Collections.copy(List<Number> dest, List<Integer> src); //Integer extends Number.
    

    As S is a parameterized type that needs binding.

    I hope this helps.

    0 讨论(0)
提交回复
热议问题