Java LinkedList.addFirst(E e)

Syntax

LinkedList.addFirst(E e) has the following syntax.

public void addFirst(E e)

Example

In the following code shows how to use LinkedList.addFirst(E e) method.


//  w  w  w .  j a  va 2s . c  om

import java.util.LinkedList;

public class Main {

   public static void main(String[] args) {

      // create a LinkedList
      LinkedList<String> list = new LinkedList<String>();

      // add some elements
      list.add("Hello");
      list.add("from java2s.com");
      list.add("10");

      // print the list
      System.out.println("LinkedList:" + list);

      // add a new element at the beggining of the list
      list.addFirst("Element");

      // print the new list
      System.out.println("LinkedList:" + list);
   }
}

The code above generates the following result.