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
🌐
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 first element of ArrayList with use of get(index) method by passing index = 0. 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 ...
🌐
Level Up Lunch
leveluplunch.com › java › examples › get-first-element-in-list
Get first element in list | Level Up Lunch
January 31, 2014 - If the arraylist is empty, an empty Optional will be returned. @Test public void get_first_element_in_list_with_java8 () { List<String> strings = new ArrayList<String>(); strings.add("one"); strings.add("two"); strings.add("three"); Optional<String> firstElement = strings.stream().findFirst(); assertEquals("one", firstElement.orElse("two")); }
🌐
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 - Create an ArrayList and add elements to it. Check whether the list is null or empty. Use get(0) to get the first element. Use get(size() - 1) to get the last element. Print both elements.
🌐
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 - Therefore, if you pass 0 to this method, you can get the first element of the current ArrayList, and if you pass list.size()-1, you can get the last element.
🌐
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 - To get the first element of a ArrayList, we can use the list.get() method by passing 0 as an argument. ... import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> list = ...
🌐
Java67
java67.com › 2015 › 06 › how-to-get-first-and-last-elements-form-ArrayList-java.html
How to get first and last elements form ArrayList in Java | Java67
List, Set, Map, ConcurrentMap, Queue, Stack, BlockingQueue, and other thread-safe collections introduced on Java 5 and 6, then I suggest you take a look at Java Generics and Collection by Maurice Naftalin and Philip Wadler, one of the best books to learn Java Collections. Here is a code example to get both the first and last elements from ArrayList in Java.
🌐
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 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 ...
🌐
Learn Java
javatutoring.com › java-get-first-element-array
Java : Return/Get First Element In Array List | 4 Ways
April 12, 2026 - Java · Get First Element Of ArrayList · This code is for displaying or printing out the first element of a user-defined array. The problem here is to print out the first element in an array whose elements are input by the user. The input here is an array of type int whose elements (i.e.
Find elsewhere
🌐
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, ArrayList’s random access complexity is O(1). In other words, calling ArrayList’s get(i) method is performant. However, not all List implementations offer O(1) random access.
🌐
W3Schools
w3schools.com › JAVA › ref_linkedlist_getfirst.asp
Java LinkedList getFirst() Method
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
CodeHS
codehs.com › editor › assignment › 16994473
ArrayList First Element | CodeHS
// to access a given index in an ArrayList // you can use the following syntax arrList.get(i); // this can also be saved to a variable int arrListElem = arrList.get(2); // arrListElem = 3 // for example, to print out the 3rd element in the ...
🌐
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 ...
🌐
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 ArrayList<String>() {{ add("rapid"); add("startup"); add("studio"); }};// print first item in the collection System.out.printf(strings.iterator().next());// print first item using java8 System.out.println(st...