Compareable<Collection<T extends Compareable<T>>>Java中是否有任何实现(表现为C++ std::list<T>::operator<()或者std::set<T>::operator<())?
编辑:Comparator会更有意义......
不是我所知道的,但写起来应该不会太难.
compareTo(Collection<T> other) {
Iterator<T> i1 = this.iterator();
Iterator<T> i2 = other.iterator();
while(i1.hasNext() && i2.hasNext()) {
int c = i1.next().compareTo(i2.next());
if(c != 0) {
return c;
}
}
if(i1.hasNext()){
return 1;
} else if(i2.hasNext()) {
return -1;
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)