[英]Java library to get Nth element from a Collection or Iterable
我有一個Collection
,需要獲取其Iterator
返回的第N個元素。 我知道我可以通過其Iterator
元素進行Iterator
。 是否有第三方庫(Google Guava或Apache Commons)這樣做?
Guava的Iterators.get
可以提供幫助
提升迭代器位置+ 1次,將元素返回到位置位置。
編輯:忽略這一點,閱讀部分問題。
你可以這樣:
for(int counter = 0; counter < max; counter++)
current = iterator.next();
之后,當前將是迭代器引起的順序的最大元素。
使用Iterators.get(Iterator iterator,int position)獲取myCollection
的第3個元素的示例:
com.google.common.collect.Iterators.get(myCollection.iterator(), 3);
在需要時使用了上面的完全限定名稱,例如在Eclipse的Display視圖中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.