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
🌐
W3Schools
w3schools.com › JAVA › ref_linkedlist_getfirst.asp
Java LinkedList getFirst() Method
Get the first item in the list: ... 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 » ·...
🌐
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?
May 19, 2025 - 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 static void main(String[] args) { //creating a list List<Integer> list = new ArrayList<>(); //adding element to it list.add(1); list.add(2); list.add(3); list.add(4); System.out.println("The list elements are: " + list); //Getting an Iterator Object Iterator<Integer> it = list.iterator(); if(it.hasNext()){ //using the next() method System.out.println("The first element in the list is: " + it.next()); } } }
🌐
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 get(int index) Parameters : index : index of the element to return Return Value : This method returns the element at the specified position in this list Exception IndexOutOfBoundsException : if the index is out of range ... // Java code to demonstrate the working // of getFirst() in linked list import java.util.*; public class LinkedListget2 { public static void main(String[] args) { // declaring a LinkedList LinkedList<String> list = new LinkedList<String>(); // adding elements using add() list.add("Geeks"); list.add("4"); list.add("Geeks"); list.add("8"); // printing the whole list System.out.println("The elements in List are : " + list); // using get() to print element at first index // prints "Geeks" System.out.println("Element at 1st index is : " + list.getFirst()); } }
🌐
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 - The exact relationship between calls on the view and calls on this List is unspecified. However, order-sensitive operations generally delegate to the appropriate method with the opposite orientation. For example, calling getFirst on the view results in a call to getLast on this List.
🌐
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
April 14, 2026 - if (!list.isEmpty()) { System.out.println("First: " + list.getFirst()); System.out.println("Last: " + list.getLast()); } Cleaner. Clearer. Consistent. ... Java 21 didn’t add these methods just for fun.
🌐
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 ...
November 19, 2023 - Java · Return the first element ... firstElement = myList.get(0); Return the first element that meets the given condition and if there is no element satisfying that condition then return Optional.empty() Available after Java ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › linkedlist-getfirst-method-in-java
LinkedList getFirst() Method in Java - GeeksforGeeks
July 11, 2025 - import java.util.LinkedList; LinkedList ... while implementing. Syntax: LinkedList.getFirst(); Return Value: The first element or the element at the head of the list....
🌐
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 - Using linkedList.getFirst(), we retrieve the first element, which is “JavaScript”. This method is particularly advantageous because it avoids the overhead of indexing, making it slightly faster than using the get() method for the first element.
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2014 › 07 › java-get-first-and-last-elements-from-linkedlist-example
Java – Get first and last elements from LinkedList example
September 11, 2022 - 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 ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-first-and-last-elements-from-arraylist-in-java
How to get first and last elements from ArrayList in Java?
September 1, 2025 - 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 = list.get(0); String lastElement = list.get(list.size()-1); System.out.println("First element: " + firstElement); } }
🌐
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_guava () { List<String> strings = Lists.newArrayList("one", "two", "three"); String firstElement = Iterables.getFirst(strings, null); assertEquals("one", firstElement); }
🌐
IncludeHelp
includehelp.com › java › linkedlist-getfirst-method-with-example.aspx
Java LinkedList getFirst() method with Example
Object getFirst(){ } Parameter(s): In this method, we don’t pass any object as a parameter in the method but it returns the first object from the linked list. Return value: The return type of this method is not void that means this method returns object or element (i.e. It returns the only ...
🌐
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
To get the first element of a List ... element of a List or Set in Java, you can use the following methods: List: List<String> list = new ArrayList<>(); // add elements to the list String first = list.get(0); Copy ·...
🌐
PrepBytes
prepbytes.com › home › java › java.util.linkedlist.get(), getfirst() and getlast() in java
Java.util.LinkedList.get(), getFirst() and getLast() in Java
March 10, 2022 - (); lst.add("Coding"); lst.add("is"); lst.add("Fun"); System.out.println("The elements present in List are : " + lst); System.out.println("Element at index 1 is : " + lst.getFirst()); } }
🌐
TutorialsPoint
tutorialspoint.com › home › java/util › java linkedlist getfirst method
Java LinkedList getFirst() Method
February 13, 2026 - The Java LinkedList getFirst() method retrieves, but does not remove, the first element of this linkedList.
🌐
Codekru
codekru.com › home › linked list – get(), getfirst() and getlast() method in java
Linked List - get(), getFirst() and getLast() method in Java - Codekru
June 7, 2022 - import java.util.LinkedList; public class Codekru { public static void main(String[] args) { LinkedList<Object> linkedList = new LinkedList<>(); // adding into the linked list linkedList.add("first"); linkedList.add("second"); linkedList.add("third"); System.out.println("First element of the linked list is: "+linkedList.getFirst()); // getting the first element present in the linked list } } Output – ·
🌐
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.