java - How to Write a generic method to find the maximal element and invoke that method? -
while trying solve exercise from generics tutorial q&a answers 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 question
option1 :would make difference if
t extends object & comparable<? super t>replacedt extends comparable<? super t>. isn'textends objectimplicit ?option2 :would make difference if
comparable<? super t>replacedcomparable<t>? if how ?eclipse code completion creates local variable
list<? extends comparable<? super comparable<? super t>>> list;on ctrl+1max(list, 1, 10);bit lengthy. how define classes (hierarchy) extendscomparable<? super t>, create list , add instances list , invoke below method ? want know how invokemax()after adding class instancesa or blistclass b extends a
write generic method find maximal element in range [begin, end) of 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); (++begin; begin < end; ++begin) if (maxelem.compareto(list.get(begin)) < 0) maxelem = list.get(begin); return maxelem; } }
would make difference if
comparable<? super t>replacedcomparable<t>? if how ?
remember comparables consumers, i.e., comparable<t> consumes t instances, should preferrable use comparable<? super t> instead of comparable<t> (quoting - pecs). make difference in case comparing type super class implements comparable<supertype>. consider 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 give type parameter t extends comparable<t>, won't able call method list<child>, child not implement comparable<child> 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 current method. child not implement comparable<child> } hence type parameter bounds should t extends comparable<? super t>.
note that, can't change child class to:
class child extends parent implements comparable<child> because in case, child class extend different instantiation of same generic type, not allowed.
would make difference if
t extends object & comparable<? super t>replacedt extends comparable<? super t>. isn't extends object implicit ?
well, there difference between 2 bounds. in 1st bound, erasure of type parameter object, whereas in 2nd bound, erasure comparable.
so, without object bound, code compile to:
public static comparable max(list list, int begin, int end) the issue might come when generifying legacy non-generic code. it's neccessary give object upper bound avoid breaking byte code compatibility. can read more on link: angelika langer - programming idioms
Comments
Post a Comment