See the javadoc

of List

list.get(0);

or Set

set.iterator().next();

and check the size before using the above methods by invoking isEmpty()

!list_or_set.isEmpty()
Answer from stacker on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-the-first-element-of-the-list-in-java
How to get the first element of the List in Java?
May 19, 2025 - In the following example, we use the get() method to retrieve the first (i.e., present at index = 0) element in the given List {10, 20, 30, 40}: import java.util.ArrayList; import java.util.List; public class findFirstElement { public static ...
🌐
Level Up Lunch
leveluplunch.com › java › examples › get-first-element-in-list
Get first element in list | Level Up Lunch
January 31, 2014 - @Test public void get_first_element_in_list_with_java () { List<String> strings = new ArrayList<String>(); strings.add("one"); strings.add("two"); strings.add("three"); String firstElement = null; if (!strings.isEmpty() && strings.size() > 0) { firstElement = strings.get(0); } assertEquals("one", firstElement); }
🌐
Blogger
javarevisited.blogspot.com › 2016 › 03 › how-to-find-first-element-of-stream-in.html
How to find the first element in Stream in Java 8? findFirst() Example
October 5, 2024 - In Java 8, you can use the Stream.findFirst() method to get the first element of Stream in Java. This is a terminal operation and is often used after applying several intermediate operations e.g. filter, mapping, flattening, etc.
🌐
Benchresources
benchresources.net › home › java › java 8 – find first and last elements in a list or arraylist ?
Java 8 – Find First and Last elements in a List or ArrayList ? - BenchResources.Net
June 21, 2022 - package in.bench.resources.find.list; ... Sharma"); names.add("Falguni Nayar"); // find First and Last element of ArrayList if(!names.isEmpty()) { first = names.get(0); last = names.get(names.size() - 1); } // print to console System.out.println("First name in the List is = " + ...
🌐
Baeldung
baeldung.com › home › java › java array › get the first n elements of a list into an array
Get the First n Elements of a List Into an Array | Baeldung
March 7, 2025 - Therefore, it’s available only for Java 8 or later version: String[] result = INPUT_LIST.stream() .limit(n) .toArray(String[]::new); assertArrayEquals(new String[] { "one", "two", "three", "four", "five" }, result); In the example above, we used the limit(n) method to make the Stream only return the first n elements from the source, INPUT_LIST.
Find elsewhere
Top answer
1 of 4
2

You can use the following by using Optional and Streams.

StudentCollection sc = ...;
final Optional<Student> student = Optional.ofNullable(sc)
    .map(StudentCollection::getStudents)
    .map(Collection::stream) 
    .flatMap(Stream::findFirst);

The optional is used to safely handle the StudentCollection or the List<Student> which might by null.


As mentioned in the comment by @GhostCat the overhead created by above snippet is horribly big. Because you're using quite expensive API-methods for such a simple task.

As @Holger suggested, you can reduce the overhead by not using the Stream-API:

StudentCollection sc = ...;
final Optional<Student> student = Optional.ofNullable(sc)
    .map(StudentCollection::getStudents)
    .filter(list -> !list.isEmpty()) 
    .map(list -> list.get(0));

Which still yields the same result as the prior snippet.


When you don't want any overhead you can use the following "old" (pre Java8) way:

StudentCollection sc = ...;
Student student = null;
if(sc != null){
    List<Student> students = sc.getStudents();
    if(students != null && !students.isEmpty()){
        student = students.get(0);
    }
}
2 of 4
1

The best code is the one which best expresses the intention of the developer. With this in mind, I would simply go for:

return yourStudentCollection != null && 
       yourStudentCollection.getStudents() != null &&
      !yourStudentCollection.getStudents().isEmpty()  ?
           yourStudentCollection.getStudents().get(0) :
           null;

This is a good fit for Optional, though. I would return an Optional<Student>:

return Optional.ofNullable(yourStudentCollection)
       .map(sc -> sc.getStudents())
       .filter(s -> !s.isEmpty())
       .map(s -> s.get(0));
🌐
W3docs
w3docs.com › home › code snippets › java › how to get the first element of the list or set?
How to get the first element of the List or Set? | W3docs
List<String> list = new ArrayList<>(); // add elements to the list String first = list.stream().findFirst().orElse(null); Set<String> set = new HashSet<>(); // add elements to the set String first = set.stream().findFirst().orElse(null);
🌐
Delft Stack
delftstack.com › home › howto › java › java get first element
How to Get First Element From List in Java | Delft Stack
March 11, 2025 - In this example, we first create an ArrayList of strings and add three programming languages to it. By calling list.get(0), we retrieve the first element, which is “Java”. The get() method is efficient and provides direct access to the element ...
🌐
Level Up Lunch
leveluplunch.com › java › examples › limit-or-take-first-elements-from-list
Take N elements from list - Java
January 29, 2014 - Example shows how to get the first number of elements from a list using a java, java8 stream.limit, Lists.subList and Guava’s Iterables.limit
🌐
W3docs
w3docs.com › home › code snippets › java › find first element by predicate
Find first element by predicate | W3docs
java · filter · findfirst · ... condition, you can use the stream() method to create a stream from the list, and then use the filter() method to specify the condition that the element should satisfy....
🌐
Medium
medium.com › @rasheedamir › java-how-to-get-first-item-from-collection-7b353ea6b2a3
Java — How to get first item from Collection? | by Rasheed Amir | Medium
August 27, 2016 - Collection<String> strings = new ...(strings.iterator().next());// print first item using java8 System.out.println(strings.stream().findFirst().orElse("not found"));...
🌐
Codemia
codemia.io › home › knowledge hub › java get first item from a collection
Java Get first item from a collection | Codemia
January 26, 2025 - To get the first item of a list, one can use the get(int index) method with 0 as the index, if the list is not empty: ... 1List<String> names = new ArrayList<>(); 2names.add("Alice"); 3names.add("Bob"); 4names.add("Charlie"); 5 6if (!names.isEmpty()) { 7 String firstItem = names.get(0); 8 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › find-first-and-last-element-of-arraylist-in-java
Find first and last element of ArrayList in java - GeeksforGeeks
July 11, 2025 - Get the last element of ArrayList ... value ArrayList System.out.print("ArrayList: " + list); // find first element int first = list.get(0); // find last element int last = list.get(list.size() - 1); // print first and last ...