sta*_*ker 288
请参阅javadoc
的列表
list.get(0);
Run Code Online (Sandbox Code Playgroud)
或设置
set.iterator().next();
Run Code Online (Sandbox Code Playgroud)
并在调用之前检查大小,然后再使用上述方法 isEmpty()
!list_or_set.isEmpty()
Run Code Online (Sandbox Code Playgroud)
小智 261
Collection c;
Iterator iter = c.iterator();
Object first = iter.next();
Run Code Online (Sandbox Code Playgroud)
(这是你最接近a的"第一"元素Set.你应该意识到它对于大多数实现来说绝对没有意义Set.这可能对LinkedHashSet和TreeSet有意义,但对HashSet没有意义.)
Son*_*123 110
在Java> = 8中,您还可以使用Streaming API:
Optional<String> first = set.stream().findFirst();
Run Code Online (Sandbox Code Playgroud)
(如果Set/List可能为空,则有用.)
Ch *_*Vas 29
让我们假设你有一个List<String> strings你想要的第一个项目.
有几种方法可以做到这一点:
Java(8之前):
String firstElement = null;
if (!strings.isEmpty() && strings.size() > 0) {
firstElement = strings.get(0);
}
Run Code Online (Sandbox Code Playgroud)
Java 8:
Optional<String> firstElement = strings.stream().findFirst();
Run Code Online (Sandbox Code Playgroud)
番石榴
String firstElement = Iterables.getFirst(strings, null);
Run Code Online (Sandbox Code Playgroud)
Apache commons(4+)
String firstElement = (String) IteratorUtils.get(strings, 0);
Run Code Online (Sandbox Code Playgroud)
Apache公共(4之前)
String firstElement = (String) CollectionUtils.get(strings, 0);
Run Code Online (Sandbox Code Playgroud)
随后或封装在适当的检查或try-catch块中.
科特林:
在Kotlin中,数组和大多数集合(例如:List)都有一个first方法调用.所以你的代码看起来像这样
列表:
val stringsList: List<String?> = listOf("a", "b", null)
val first: String? = stringsList.first()
Run Code Online (Sandbox Code Playgroud)
对于数组:
val stringArray: Array<String?> = arrayOf("a", "b", null)
val first: String? = stringArray.first()
Run Code Online (Sandbox Code Playgroud)
Rad*_*icz 19
我很惊讶没有人建议番石榴解决方案:
com.google.common.collect.Iterables.get(collection, 0)
// or
com.google.common.collect.Iterables.get(collection, 0, defaultValue)
// or
com.google.common.collect.Iterables.getFirst(collection, defaultValue)
Run Code Online (Sandbox Code Playgroud)
或者如果您期望单个元素:
com.google.common.collect.Iterables.getOnlyElement(collection, defaultValue)
// or
com.google.common.collect.Iterables.getOnlyElement(collection)
Run Code Online (Sandbox Code Playgroud)
Vas*_*hev 11
组
set.toArray()[0];
Run Code Online (Sandbox Code Playgroud)
名单
list.get(0);
Run Code Online (Sandbox Code Playgroud)
java8等等
Set<String> set = new TreeSet<>();
set.add("2");
set.add("1");
set.add("3");
String first = set.stream().findFirst().get();
Run Code Online (Sandbox Code Playgroud)
这将帮助您检索列表或集的第一个元素.假设set或list不为空(get()在空可选时将抛出java.util.NoSuchElementException)
orElse() 可以用作:(这只是一种解决方法 - 不推荐)
String first = set.stream().findFirst().orElse("");
set.removeIf(String::isEmpty);
Run Code Online (Sandbox Code Playgroud)
以下是适当的方法:
Optional<String> firstString = set.stream().findFirst();
if(firstString.isPresent()){
String first = firstString.get();
}
Run Code Online (Sandbox Code Playgroud)
类似地,可以检索列表的第一个元素.
希望这可以帮助.
这不是这个问题的精确答案,但是如果对象应该排序,SortedSet有一个first()方法:
SortedSet<String> sortedSet = new TreeSet<String>();
sortedSet.add("2");
sortedSet.add("1");
sortedSet.add("3");
String first = sortedSet.first(); //first="1"
Run Code Online (Sandbox Code Playgroud)
排序的对象必须实现Comparable接口(就像String一样)
| 归档时间: |
|
| 查看次数: |
475400 次 |
| 最近记录: |