This example shows how to get elements from the LinkedList using the get method. The example also shows how to get elements using the peek, peekFirst, peekLast, poll, pollFirst, and pollLast methods.
How to get elements from the LinkedList in Java?
There are several ways using which you can get elements from the LinkedList in Java.
1. Using the get method
The get method of the LinkedList class returns an element of the linked list located at the specified index.
1 |
public E get(int index) |
The get method returns an element at the specified index of the LinkedList object.
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 |
import java.util.LinkedList; public class LinkedListGetElementsExample { public static void main(String[] args) { LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); /* * To get elements from the LinkedList, use * the get method and specify the index */ //this will return "One" i.e. element at index 0 System.out.println( linkedListNumbers.get(0) ); //this will return "Three" i.e. element at index 2 System.out.println( linkedListNumbers.get(2) ); } } |
Output
1 2 |
One Three |
Please remember that the index of the LinkedList starts from 0 and ends at the linked list size – 1 index. In other words, the first element of the linked list is located at index 0 and the last element is located at index list size – 1.
If the specify an index which is out of the range of the list, the get
method throws IndexOutOfBoundsException exception. The index is considered out of the range it is less than 0 or it is greater than or equal to the list size.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); /* * this will throw IndexOutOfBoundsException because * it is out of the range which is 0 to 2 */ System.out.println( linkedListNumbers.get(3) ); |
Output
1 2 3 |
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.LinkedList.checkElementIndex(Unknown Source) at java.util.LinkedList.get(Unknown Source) |
Always make sure to check the LinkedList size before accessing its elements using the get
method to avoid this exception.
2. Get elements using the getFirst and getLast methods
The getFirst
and getLast
methods of the LinkedList class return the first and last elements of the linked list respectively.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); //this will return "One" i.e. first element System.out.println( linkedListNumbers.getFirst() ); //this will return "Three" i.e. last element System.out.println( linkedListNumbers.getLast() ); |
Output
1 2 |
One Three |
Please visit how to get first and last elements from the LinkedList example to know more.
3. Get elements using the peek, peekFirst and peekLast methods
The peek
and peekFirst
methods return the first element of the LinkedList while the peekLast
method returns the last element of the linked list object.
1 |
public E peek() |
1 |
public E peekFirst() |
1 |
public E peekLast() |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); //this will retrun the "One" System.out.println( linkedListNumbers.peek() ); //this will also return "One" System.out.println( linkedListNumbers.peekFirst() ); //this will return "Three" System.out.println( linkedListNumbers.peekLast() ); |
Output
1 2 3 |
One One Three |
4. Using the poll, pollFirst, and pollLast methods
The poll
and pollFirst
method retrieve and remove the first element of the linked list while the pollLast
method retrieves and removes the last element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); linkedListNumbers.add("Four"); linkedListNumbers.add("Five"); //this will remove and retrun the "One" System.out.println( linkedListNumbers.poll() ); //this will remove and return "Two" System.out.println( linkedListNumbers.pollFirst() ); //this will remove and return "Five" System.out.println( linkedListNumbers.pollLast() ); |
Output
1 2 3 4 |
One Two Five LinkedList contains: [Three, Four] |
Important Note: The poll
, pollFirst
, and pollLast
methods also remove the element from the LinkedList.
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java LinkedList Javadoc