Prashant Bhate
Prashant Bhate

Reputation: 11087

How to Write a generic method to find the maximal element and invoke that method?

While I was trying to solve exercise from generics tutorial Q&A My answers were slightly different

My Answers

public static <T extends Comparable<? super T>>
    T max(List<? extends T> list, int begin, int end) //Option1

public static <T extends Comparable<T>>
    T max(List<? extends T> list, int begin, int end) //Option2

from quoted answer below

So My question is


Write a generic method to find the maximal element in the range [begin, end) of a list.

Answer:

import java.util.*;

public final class Algorithm {
    public static <T extends Object & Comparable<? super T>>
        T max(List<? extends T> list, int begin, int end) {

        T maxElem = list.get(begin);

        for (++begin; begin < end; ++begin)
            if (maxElem.compareTo(list.get(begin)) < 0)
                maxElem = list.get(begin);
        return maxElem;
    }
}

Upvotes: 9

Views: 3368

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213311

Would it make any difference if Comparable<? super T> is replaced with Comparable<T> ? if so How ?

Remember that Comparables are always consumers, i.e., a Comparable<T> consumes T instances, so it should always be preferrable to use Comparable<? super T> instead of Comparable<T> (Quoting - PECS). It would make difference in case you are comparing a type whose super class implements a Comparable<SuperType>. Consider the following code:

class Parent implements Comparable<Parent> {
    protected String name;

    @Override
    public int compareTo(Parent o) {
        return this.name.compareTo(o.name);
    }
}

class Child extends Parent {
    public Child(String name) {
        this.name = name;
    }
}

Now if you give your type parameter as T extends Comparable<T>, you won't be able to call that method for List<Child>, as Child does not implement Comparable<Child> but Comparable<Parent>:

public static <T extends Comparable<T>> T max(List<? extends T> list, int begin, int end) {
    ...
}

public static void main(String[] args) {
    List<Child> list = new ArrayList<Child>();
    max(list, 0, 2);  // Error with current method. Child does not implement Comparable<Child>
}

Hence the type parameter bounds should be T extends Comparable<? super T>.

Note that, you can't change your Child class to:

class Child extends Parent implements Comparable<Child>

because in that case, Child class would extend from different instantiation of same generic type, which is not allowed.


Would it make any difference if T extends Object & Comparable<? super T> is replaced with T extends Comparable<? super T>. Isn't extends Object implicit ?

Well, there is a difference between the two bounds. In the 1st bound, the erasure of the type parameter is Object, whereas in the 2nd bound, the erasure is Comparable.

So, without Object bound, your code will compile to:

public static Comparable max(List list, int begin, int end)

The issue might come when you are generifying the legacy non-generic code. It's neccessary to give Object also as upper bound to avoid breaking the Byte Code compatibility. You can read more about it on this link: Angelika Langer - Programming Idioms

Upvotes: 8

ZhongYu
ZhongYu

Reputation: 19682

It looks like there are more wildcards than necessary. I'll probably go with

public static <T extends Comparable<? super T>>
    T max(List<T> list, int begin, int end)

An even more constrained version:

public static <T extends C, C extends Comparable<C>>
    T max(List<T> list, int begin, int end)

i.e. T must have a super type that is comparable to itself. For example T cannot be Foo

    class Foo implements Comparable<Object>

Foo does not make sense anyway; a Comparable can only be meaningfully compared to its own kind. The clause C extends Comparable<C> acknowledges that fact.

Upvotes: 0

Related Questions