- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java LinkedList addFirst() Method
Description
The Java LinkedList addFirst(E e) method inserts the specified element E at the front of the linkedList. This method helps in setting the the first element in the list during the course of various add operations.
Declaration
Following is the declaration for java.util.LinkedList.addFirst() method −
public void addFirst(E e)
Parameters
e − The element to be added in the linkedList.
Return Value
This method does not return any value.
Exception
NullPointerException − if the specified element is null.
Adding an Element to the Start of the LinkedList of Integers Example
The following example shows the usage of Java LinkedList addFirst(E) method. In this example, we're using integers. As first, we'll add some items to the linkedList using add() method and then using addFirst() method, we'll add elements to the front of the linkedList. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the linkedList are in the order we desired.
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<Integer> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add(4); linkedList.add(5); linkedList.add(6); // use addFirst() method to add element at the front of the linkedList linkedList.addFirst(3); linkedList.addFirst(2); linkedList.addFirst(1);//now, element 1 will be at the front // these elements will be added in continuation with linkedList.add(6) linkedList.add(7); linkedList.add(8); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); } }
Output
Let us compile and run the above program, this will produce the following result −
LinkedList = [1, 2, 3, 4, 5, 6, 7, 8]
Adding an Element to the Start of the LinkedList of Strings Example
The following example shows the usage of Java LinkedList addFirst(E) method. In this example, we're using strings. As first, we'll add some items to the linkedList using add() method and then using addFirst() method, we'll add elements to the front of the linkedList. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the linkedList are in the order we desired.
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<String> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add("D"); linkedList.add("E"); linkedList.add("F"); // use addFirst() method to add element at the front of the linkedList linkedList.addFirst("C"); linkedList.addFirst("B"); linkedList.addFirst("A");//now, element A will be at the front // these elements will be added in continuation with linkedList.add("F") linkedList.add("G"); linkedList.add("H"); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); } }
Output
Let us compile and run the above program, this will produce the following result −
LinkedList = [A, B, C, D, E, F, G, H]
Adding an Element to the Start of the LinkedList of Objects Example
The following example shows the usage of Java LinkedList addFirst(E) method. In this example, we're using Student objects. As first, we'll add some items to the linkedList using add() method and then using addFirst() method, we'll add elements to the front of the linkedList. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the linkedList are in the order we desired.
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<Student> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add(new Student(4, "Julie")); linkedList.add(new Student(5, "Robert")); linkedList.add(new Student(6, "Adam")); // use addFirst() method to add element at the front of the linkedList linkedList.addFirst(new Student(3, "Rohan")); linkedList.addFirst(new Student(2, "Sohan")); linkedList.addFirst(new Student(1, "Mohan"));//now, Student 1 will be at the front // these elements will be added in continuation with linkedList.add(new Student(6, "Adam")) linkedList.add(new Student(7, "Ali")); linkedList.add(new Student(8, "Ahmad")); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result −
LinkedList = [[ 1, Mohan ], [ 2, Sohan ], [ 3, Rohan ], [ 4, Julie ], [ 5, Robert ], [ 6, Adam ], [ 7, Ali ], [ 8, Ahmad ]]
To Continue Learning Please Login
Login with Google