提交 4917c012 编写于 作者: K kohsuke

added a method to reverse a collection.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@5440 71c3de6d-444a-0410-be80-ed276b4c234a
上级 8566c115
......@@ -2,7 +2,9 @@ package hudson.util;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.ListIterator;
/**
* Varios {@link Iterator} implementations.
......@@ -49,4 +51,28 @@ public class Iterators {
throw new UnsupportedOperationException();
}
}
/**
* Returns the {@link Iterable} that lists items in the reverse order.
*/
public static <T> Iterable<T> reverse(final List<T> lst) {
return new Iterable<T>() {
public Iterator<T> iterator() {
final ListIterator<T> itr = lst.listIterator(lst.size());
return new Iterator<T>() {
public boolean hasNext() {
return itr.hasPrevious();
}
public T next() {
return itr.previous();
}
public void remove() {
itr.remove();
}
};
}
};
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册