Java linkedlist addfirst – In the previous article, we have discussed about Java LinkedList addAll() Method with Examples
In this article we are going to see the use of Java LinkedList addFirst() method with suitable examples.
Java LinkedList addFirst() Method with Examples
Linkedlist addfirst: This java.util.LinkedList.addFirst()
method adds/inserts an element to the LinkedList at the first index position.
It always returns true as the collection need a return value in the signature when an element is added.
If we don’t insert any element inside the addFirst() method then it will show NullPointerException
.
Syntax:
LinkedListName.addFirst(Object element)
Where,
LinkedListName
refers to the name of your LinkedList.Object element
refers to the element that will be added into the LinkedList.int index
refers to the index value of the object element.
Let’s see different examples to understand it more clearly.
Example-1: Java LinkedList addFirst() Method – Example with String Type LinkedList
- Create a new LinkedList of type String.
- Add string elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Add another new element to the LinkedList using addFirst( ).
- Print the new LinkedList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of string datatype LinkedList<String> l = new LinkedList<String>(); // Adding some elements to the LinkedList l.add("Hello"); l.add("this"); l.add("is"); l.add("a"); l.add("Linked List"); // Prints the LinkedList elements System.out.println("The elements in the LinkedList are: "+l); // Adding a new element to the LinkedList l.addFirst("Welcome"); // Prints the new LinkedList elements System.out.println("The new elements in the LinkedList are: "+l); } }
Output: The elements in the LinkedList are: [Hello, this, is, a, Linked List] The new elements in the LinkedList are: [Welcome, Hello, this, is, a, Linked List]
Example-2: Java LinkedList addFirst() Method – Example with Integer Type LinkedList
- Create a new LinkedList of type Integer.
- Add Integer elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Add another new element to the LinkedList using addFirst( ).
- Print the new LinkedList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of Integer datatype LinkedList<Integer> l = new LinkedList<Integer>(); // Adding some elements to the LinkedLists l.add(2); l.add(52); l.add(13); l.add(17); l.add(1); // Prints the LinkedList elements System.out.println("The elements in the LinkedList are: "+l); // Adding a new element to the LinkedList l.addFirst(99); // Prints the new LinkedList elements System.out.println("The new elements in the LinkedList are: "+l); } }
Output: The elements in the LinkedList are: [2, 52, 13, 17, 1] The new elements in the LinkedList are: [99, 2, 52, 13, 17, 1]
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Programs: