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 › how-to-get-the-first-element-of-the-list-in-java
How to get the first element of the List in Java?
In the example below, we use the iterator() method to get an iterator object on the ArrayList, and use the next() method to get the first element of this List {1, 2, 3, 4, 5}: import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class findFirstElement { public ...
🌐
W3Schools
w3schools.com › JAVA › ref_linkedlist_getfirst.asp
Java LinkedList getFirst() Method
Java Examples Java Videos Java ... cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); // Use getFirst() to get the first item in the list System.out.println(cars.getFirst()); } } Try it Yourself » ·...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › List.html
List (Java SE 21 & JDK 21)
January 20, 2026 - Gets the first element of this collection. ... If this List is not empty, the implementation in this interface returns the result of calling get(0).
🌐
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 - Learn the steps to retrieve the first n elements from a List and convert them into an array in Java.
🌐
W3Docs
w3docs.com › java
How to get the first element of the List or Set?
Note that this will throw an IndexOutOfBoundsException if the list is empty. ... Note that this will throw a NoSuchElementException if the set is empty. Alternatively, you can use the stream() method to get the first element of a List or Set in a more concise way:
🌐
Level Up Lunch
leveluplunch.com › java › examples › get-first-element-in-list
Get first element in list | Level Up Lunch
January 31, 2014 - This snippet will find the first element in arraylist using java 8. The Java 8 Streams API provides Streams.findFirst which will return the wrapper Optional object describing the first element of the stream.
🌐
TWpower's Tech Blog
twpower.github.io › 328-difference-between-get-and-findfirst-in-java-list-en
[Java](EN) Difference between get(0) and findFirst() in ...
List<String> myList = Arrays.asList("1", "2", "3"); Optional<String> firstElementOptional = myList.stream().findFirst(); if (firstElementOptional.isPresent()) { String firstElement = firstElementOptional.get(); }
Find elsewhere
🌐
Coderanch
coderanch.com › t › 392457 › java › Retrieving-item-list
Retrieving the first item in list (Beginning Java forum at Coderanch)
The items are added from the list position of 1, and get() throws an exception if I specify get(0). The purpose of the class is to sum the items in the list.
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-first-and-last-elements-from-arraylist-in-java
How to get first and last elements from ArrayList in Java?
Following is the Java program to get the first and last elements from an ArrayList using the for loop: import java.util.ArrayList; public class GetTheFirstandLastEl{ public static void main(String[] args){ ArrayList<String> list = new ArrayList<>(); list.add("Banana"); list.add("Apple"); list.add("Mango"); list.add("Orange"); list.add("Grapes"); list.add("Pineapple"); String firstElement = ""; String lastElement = ""; for(int i=0; i<list.size(); i++){ if(i == 0){ firstElement = list.get(i); } if(i == list.size()-1){ lastElement = list.get(i); } } System.out.println("First element: " + firstElement); } }
🌐
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 with use of get(index) method by passing index = size - 1. Below is the implementation of the above approach: Java · // Java code to find first and last element // of ArrayList import java.util.ArrayList; public ...
🌐
Medium
medium.com › @manishkumar.modi › why-java-21-added-getfirst-and-friends-to-arraylist-even-though-they-just-call-old-methods-d08c9d4e4b6c
🧭 Why Java 21 Added getFirst() and Friends to ArrayList — Even Though They Just Call Old Methods | by Manishkumar Modi | Medium
October 18, 2025 - public interface SequencedCollection<E> extends Collection<E> { E getFirst(); E getLast(); void addFirst(E e); void addLast(E e); E removeFirst(); E removeLast(); SequencedCollection<E> reversed(); } The idea is simple but important to provide a consistent, unified API for ordered collections. Before Java 21, each type of collection handled order differently:
🌐
Reactgo
reactgo.com › home › how to get the first element of an arraylist in java
How to get the first element of an ArrayList in Java | Reactgo
August 22, 2023 - In this tutorial, we are going to learn about how to get the first element of an ArrayList in Java. ... To get the first element of a ArrayList, we can use the list.get() method by passing 0 as an argument.
🌐
Baeldung
baeldung.com › home › java › java collections › how to get first item from a java set
How to Get First Item From a Java Set | Baeldung
June 27, 2025 - A Java Set is unordered by definition, however, there are implementations that do maintain order, such as LinkedHashSet, which we’ll focus on here. We can retrieve the first element in a Set using an Iterator. The Set interface allows us to ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-linkedlist-get-getfirst-getlast-java
Java.util.LinkedList.get(), getFirst(), getLast() in Java - GeeksforGeeks
June 24, 2024 - Declaration : public E getFirst() Return Value : This method returns the first element in this list Exceptions : NoSuchElementException : if this list is empty ... // Java code to demonstrate the working // of getFirst() in linked list import ...
🌐
BeginnersBook
beginnersbook.com › 2014 › 07 › java-get-first-and-last-elements-from-linkedlist-example
Java – Get first and last elements from LinkedList example
Method definition and description are as follows: 1) public E getFirst(): Returns the first element in this list. 2) public E getLast(): Returns the last element in this list. import java.util.LinkedList; public class GetFirstAndLast { public static void main(String[] args) { // Create a LinkedList ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › get-first-and-last-elements-from-arraylist-in-java
Get first and last elements from ArrayList in Java - GeeksforGeeks
July 11, 2025 - But remember, that you need to use size() method for ArrayList, not length, length is used to get the length of an array. Find First and Last in ArrayList: JAVA · // java program print first and last element of a List import java.util.ArrayList; import java.util.List; public class FindFirstLast { public static void getFirstLat(List<Integer> list) { // Displaying ArrayList elements System.out.println("ArrayList contains: " + list); // Logic to get the last element from ArrayList if (list != null && !list.isEmpty()) { System.out.println("First element is: " + list.get(0)); System.out.println("L