Is possibe to retrive the objects of a LinkedList without sorting it?
class MyClass<T> implements Iterable<T> {
private LinkedList<T> myList = new LinkedList<>();
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
// SHOULD RETURN THE ELEMENTS OF MYLIST IN A SORTED WAY
return null;
}
};
}
}
In this case we can assume that objects of type T have an Integer field for sorting
It's not possible, unless you create extra methods to sort 'on-the-fly' or store a pre-ordered list in another object (I'm assuming you dont want to change the original list order).
Both methods have costs:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With