I want to do something like this
private <T extends List<?>> List<?> getFirstFiveElements(T list) {
//body
}
However when I tray to pass in the arguments it is not working
List<A> a = new LinkedList<A>;
List<B> b = new LinkedList<B>;
getFirstFiveElements(a);
getFirstFiveElements(b);
so any suggestion how I cam make this work.
The following should work just fine.
Tserves no useful purpose here, and therefore can be eliminated.If you want the method to return the same type that it takes (i.e.
ArrayListforArrayList,LinkedListforLinkedList), then sorry to tell you, Java’s type system is not capable of that.