We have seen, creation of Java LinkedList, LinkedList insertion, and How to perform deletion in linked list. In this post, we will see how to retrieve elements from LinkedList or get the element from LinkedList, and see different methods in java LinkedList get the node. There are some methods that retrieve and remove the element at the same time.
Here is the table content of the article will we will cover this topic.
1. get(int index) method
2. getFirst() method
3. getLast() method
4. peek() method
5. peekFirst() method
6. peekLast() method
7. element() method
There are other methods that are used to get and remove the element. You can read them from here.
1. get(int index) method
The get(int index) method is used to get the specific element from the LinkedList. This method takes one parameter of int type that is index value and returns the object that presents on the index. It throws IndexOutOfBoundsException if index value is not correct or out of bound.
public E get(int index)
Where, E represents the type of elements in LinkedList.
index, index of the element to return.
throw, IndexOutOfBoundsException if index is invalid.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); // It returns the element present at index 1 System.out.println("Element present at index 1 is = "+listOfNames.get(1)); } }
Output: Element present at index 1 is = Goal
2. getFirst() method
The getFirst() method returns the head of the LinkedList. It doesn’t take any parameter and returns the first element from LinkedList. It throws NoSuchElementException if LinkedList is empty.
E getFirst();
Where E represents the type of elements in LinkedList.
throws NoSuchElementException if this LinkedList is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); System.out.println("Element from getFirst() = "+listOfNames.getFirst()); } }
Output: Element from getFirst() = Java
3. getLast() method
The getLast() method returns the tail of the LinkedList. This method doesn’t take any parameter and returns the last element from the LinkedList. If LinkedList is empty it will throw NoSuchElementException.
E getLast();
Where E represents the type of elements in LinkedList.
throws NoSuchElementException if this LinkedList is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); System.out.println("Element from getLast() = "+listOfNames.getLast()); } }
Output: Element from getLast() = Website
4. peek() method
The peek() method is declared in Queue interface. It is also return the first element(Head) from the LinkedList. But if LinkedList is empty it returns null.
E peek();
Where E represents the type of elements in LinkedList.
return null, if this LinkedList is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); System.out.println("Element from peek() = "+listOfNames.peek()); } }
Output: Element from peek() = Java
5. peekFirst() method
The peekFirst() method return the head of the LinkedList. This method is used get the first element from LinkedList. It just Retrieve the element, but does not remove.
E peekFirst();
Where, E represents the type of elements in LinkedList.
return null, if this LinkedList is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); System.out.println("Element from peekFirst() = "+listOfNames.peekFirst()); } }
Element from peekFirst() = Java
6. peekLast() method
The peekLast() method is used get the last element(tail) from LinkedList. It just Retrieve the element, but does not remove.
E peekLast();
Where, E represents the type of elements in LinkedList.
return null, if this LinkedList is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); System.out.println("Element from peekLast() = "+listOfNames.peekLast()); } }
Output: Element from peekLast() = Website
7. element() method
The element() method is defined in LinkedList class. This method returns the first element from LinkedList. But doesn’t remove the element from list. It returns the object.
E element();
Where, E represents the type of elements in LinkedList.
return, First element from the LinkedList.
throws NoSuchElementException if this list is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("Java"); listOfNames.add("Goal"); listOfNames.add("Learning"); listOfNames.add("Website"); System.out.println("Element from element = "+listOfNames.element()); } }
Output: Element from element = Java