This example shows how to get the first element and last element of the LinkedList in Java. This example also shows how to get the first and last element using the getFirst and getLast methods.
How to get the first and last elements of the LinkedList in Java?
1. Using the index
The LinkedList is an index based linked list implementation of the List interface. Since it is an index-based collection, its elements can be accessed using the index.
The List index starts from 0 and ends at the list size-1 index. In other words, the first element of the LinkedList is located at index 0 and the last element of the LinkedList is located at the index list size – 1.
The below given example shows how to get the first and last elements from the linked list using the index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.LinkedList; public class LinkedListGetFirstLastElementsExample { public static void main(String[] args) { //create new LinkedList object LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); //add elements linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); linkedListNumbers.add(4); linkedListNumbers.add(5); /* * First element of the linked list is * located at index 0 */ if(linkedListNumbers.size() > 0) System.out.println( "First element: " + linkedListNumbers.get(0) ); /* * Last element of the linked list is * located at index list size - 1. */ if(linkedListNumbers.size() > 0) System.out.println( "Last element: " + linkedListNumbers.get( linkedListNumbers.size() - 1 ) ); } } |
Output
1 2 |
First element: 1 Last element: 5 |
Note:
Always check the linked list size before calling the get
method using the index.
The get
method of the LinkedList class throws the IndexOutOfBoundsException if the specified index is out of the range of the list. That is if the specified index is less than 0 or greater than or equal to the list size.
2. Using the getFirst and getLast methods
You can also use the getFirst
and getLast
methods of the LinkedList class to access the first and last elements of the linked list respectively.
1 |
public E getFirst() |
1 |
public E getLast() |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.LinkedList; public class LinkedListGetFirstLastElementsExample { public static void main(String[] args) { //create new LinkedList object LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); //add elements linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); linkedListNumbers.add(4); linkedListNumbers.add(5); if(linkedListNumbers.size() > 0) System.out.println( "First element: " + linkedListNumbers.getFirst() ); if(linkedListNumbers.size() > 0) System.out.println( "Last element: " + linkedListNumbers.getLast() ); } } |
Output
1 2 |
First element: 1 Last element: 5 |
The getFirst
and getLast
methods throw NoSuchElementException exception if the list is empty. That is why it is necessary to check if the linked list is empty before calling these methods.
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList