Java LinkedHashSet向后迭代 [英] Java LinkedHashSet backwards iteration
本文介绍了Java LinkedHashSet向后迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从最后一项到第一项迭代 LinkedHashSet
的项?
How can I iterate through items of a LinkedHashSet
from the last item to the first one?
推荐答案
如果您想继续使用集合,可以使用以下内容:
If you want to continue to use collections, you could use the following:
LinkedHashSet<T> set = ...
LinkedList<T> list = new LinkedList<>(set);
Iterator<T> itr = list.descendingIterator();
while(itr.hasNext()) {
T item = itr.next();
// do something
}
如果你使用的话没问题相反,您可以查看 hvgotcodes的答案。
If you're fine with using an array instead, you could take a look at hvgotcodes' answer.
这篇关于Java LinkedHashSet向后迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文