Add element to LinkedList

boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
void addFirst(E e)
Inserts the specified element at the beginning of this list.
void addLast(E e)
Appends the specified element to the end of this list.
boolean offer(E e)
Adds the specified element as the tail (last element) of this list.
boolean offerFirst(E e)
Inserts the specified element at the front of this list.
boolean offerLast(E e)
Inserts the specified element at the end of this list.

  import java.util.LinkedList;

public class Main{
    public static void main(String args[]) {

        LinkedList<String> ll = new LinkedList<String>();

        ll.add("A");
        ll.add("B");
        ll.add("ja v a2s.com");
        ll.add("E");
        ll.add("F");

        ll.add(1, "A2");

        System.out.println("Original contents of ll: " + ll);

    }
}
  

The output:


Original contents of ll: [A, A2, B, ja v a2s.com, E, F]

The following code add elements to the start of the LinkedList.


  import java.util.LinkedList;

public class Main{
    public static void main(String args[]) {

        LinkedList<String> ll = new LinkedList<String>();


        ll.add("B");
        ll.add("ja v a2s.com");
        ll.addLast("E");
        ll.add("F");

        ll.add(1, "A2");
        ll.addFirst("A");
        System.out.println("Original contents of ll: " + ll);

    }
}
  

The output:


Original contents of ll: [A, B, A2, ja v a2s.com, E, F]

Storing User-Defined Classes in LinkedList

 
import java.util.Iterator;
import java.util.LinkedList;
class Address {
  private String name;
  private String street;

  Address(String n, String s) {
    name = n;
    street = s;
  }

  public String toString() {
    return name + "\n" + street + "\n";
  }
}

public class Main{
  public static void main(String args[]) {
    LinkedList ml = new LinkedList();
    ml.add(new Address("Jack", "11 Oak Ave"));
    ml.add(new Address("Tom", "1142 Maple St"));
    ml.add(new Address("Mary", "867 Elm St"));

    Iterator itr = ml.iterator();
    while (itr.hasNext()) {
      Object element = itr.next();
      System.out.println(element + "\n");
    }
  }
}

The output from the program is shown here:


Jack
11 Oak Ave


Tom
1142 Maple St


Mary
867 Elm St
Home 
  Java Book 
    Collection  

LinkedList:
  1. LinkedList class
  2. Create LinkedList
  3. Add element to LinkedList
  4. Remove all elements from LinkedList
  5. Shallow copy of a LinkedList
  6. If contain a certain element
  7. Get iterator from LinkedList
  8. Peek the element
  9. Get the element from LinkedList
  10. Get the index of an element
  11. Poll, pop and push element to a LinkedList
  12. Remove element from a LinkedList
  13. Replace the element at the position
  14. Get the size of a LinkedList
  15. Convert LinkedList to Array
  16. Storing User-Defined Classes in Collections