Java:从集合中获取第一项 [英] Java: Get first item from a collection
本文介绍了Java:从集合中获取第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个集合,比如Collection
,我怎样才能取出第一个项目?我可以只调用一个 Iterator
,取出它的第一个 next()
,然后将 Iterator
扔掉.有没有什么不那么浪费的方法呢?
If I have a collection, such as Collection<String> strs
, how can I get the first item out? I could just call an Iterator
, take its first next()
, then throw the Iterator
away. Is there a less wasteful way to do it?
推荐答案
Iterables.get(yourC, indexYouWant)
因为实际上,如果您使用的是收藏集,则应该使用 Google 收藏集.
Because really, if you're using Collections, you should be using Google Collections.
这篇关于Java:从集合中获取第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文