I have a problem with the general method after switching to Java 1.8, which was good with Java 1.6 and 1.7. Consider the following code:
public class ExtraSortList<E> extends ArrayList<E> {
ExtraSortList(E... elements) {
super(Arrays.asList(elements));
}
public List<E> sortedCopy(Comparator<? super E> c) {
List<E> sorted = new ArrayList<E>(this);
Collections.sort(sorted, c);
return sorted;
}
public static void main(String[] args) {
ExtraSortList<String> stringList = new ExtraSortList<>("foo", "bar");
Comparator<? super String> compGen = null;
String firstGen = stringList.sortedCopy(compGen).get(0);
Comparator compRaw = null;
String firstRaw = stringList.sortedCopy(compRaw).get(0);
}
}
I tried this with Oracle javac (1.8.0_92) and with the Eclipse JDT compiler (4.6.1). This is the same result for both. (the error message is slightly different, but essentially the same)
Besides the fact that you can prevent errors by avoiding raw types, this puzzles me because I do not understand the reason.
Why does the raw parameter of sortedCopy-Method have any effect on the overall return type? A generic type is already defined at the class level. The method does not define a separate generic type. The link listis printed on <String>, so there should be a returned list.
Java 8 ?
EDIT: sortedCopy ( biziclop)
public List<E> sortedCopy(Comparator c) {
E ExtraSortList<E> . c , , , .
EDIT: Java Language Specification, , . :
E - ExtraSortList, sortedCopy.sortedCopy , E . . JLS- JLS
, (§18 ( )).
stringList String, E sortedCopy, .stringList reified E, c Comparator<? super String> .E, List<String>.
, , Java . , , , .