
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get First Element of the List in Java
A list stores a sequence of elements of a similar type. Like an array, the elements in a List are stored at specific indices, starting from index 0. The 0th index indicates the "first element", the 1st indicates the "second" element, and so on.
In Java, a list is represented by the interface named List (with the same name) that extends the Collection interface. To create a List object, we can instantiate any class that implements the List interface, such as ArrayList, Stack, Vector, etc (Since we cannot instantiate an interface).
We can get the first element of the List in Java -
- Using the get() Method of List Interface
- Using Iterator Object
First Element of the List using the get() Method
The get() method of the List interface accepts an index as a parameter and returns the element at the specified position.
Since the elements in the List are stored at a specific index starting at 0. If you specify the index value as 0, this method will return the first element in the given List. Following is the syntax of the get() method:
get(int index)
Here, the index is the position of an element in the List.
Example
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 void main(String[] args) { //creating a list List<Integer> list = new ArrayList<>(); //adding element to it list.add(10); list.add(20); list.add(30); list.add(40); System.out.println("The list elements are: " + list); int index = 0; System.out.println("The index is: " + index); //using the get() method System.out.println("The first element in the list is: " + list.get(index)); } }
The above program produces the following output:
The list elements are: [10, 20, 30, 40] The index is: 0 The first element in the list is: 10
Using an Iterator
All the implementing classes of the List interface provide an Iterator object. An Iterator object is used to iterate through the contents of the current object. It provides methods such as next() and hasNext() to do so.
You can get an iterator object on the ArrayList using the iterator() method.
Example
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()); } } }
Following is the output of the above program:
The list elements are: [1, 2, 3, 4] The first element in the list is: 1
Example
While retrieving an element from a List, we need to ensure that the List is not empty(), otherwise, we may get an "IndexOutOfBoundsException".
import java.util.ArrayList; import java.util.List; public class findFirstElement { public static void main(String[] args) { //creating a List List<Integer> list = new ArrayList<>(); System.out.println("The list is: " + list); try { // First element of the List System.out.println("The first element of the List: " + list.get(0)); } catch(Exception e) { e.printStackTrace(); } } }
Output
This will produce the following result:
The list is: [] java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0