As I have read in may places, Java 8 static methods in interfaces, is meant for avoiding the case where we have to write a separate package-private class for utility methods. However, when I examine Java 8 Collections class, I see it is still having some utility methods which could be moved to collection interface.
for instance the reverse method is still in Collection class. and there are other examples as well. I would like to know, why some methods are moved to collection interface while others are still kept inside Collections utility class.
@SuppressWarnings({"rawtypes", "unchecked"})
public static void reverse(List<?> list) {
int size = list.size();
if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
swap(list, i, j);
} else {
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
ListIterator fwd = list.listIterator();
ListIterator rev = list.listIterator(size);
for (int i=0, mid=list.size()>>1; i<mid; i++) {
Object tmp = fwd.next();
fwd.set(rev.previous());
rev.set(tmp);
}
}
}
Aucun commentaire:
Enregistrer un commentaire