How do I add item at the beginning or the end of LinkedList?

To add an item before the first item in the linked list we use the LinkedList.addFisrt() method and to add an items after the last item in the linked list we use the LinkedList.addLast() method.

package org.kodejava.util;

import java.util.LinkedList;

public class LinkedListAddItems {
    public static void main(String[] args) {
        LinkedList<String> grades = new LinkedList<>();
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        System.out.println("Original values are:");
        System.out.println("====================");
        for (String grade : grades) {
            System.out.println("Grade = " + grade);
        }

        grades.addFirst("A");
        grades.addLast("F");

        System.out.println("New values are:");
        System.out.println("====================");
        for (String grade : grades) {
            System.out.println("Grade = " + grade);
        }
    }
}

The result of the program are the following:

Original values are:
====================
Grade = B
Grade = C
Grade = D
Grade = E
New values are:
====================
Grade = A
Grade = B
Grade = C
Grade = D
Grade = E
Grade = F

How do I remove the first and last item from LinkedList?

To remove the first or the last elements from the linked list we can use the removeFirst() and removeLast() methods provided by the LinkedList class.

package org.kodejava.util;

import java.util.LinkedList;

public class LinkedListRemoveItems {
    public static void main(String[] args) {
        LinkedList<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");
        grades.add("F");

        System.out.println("Original values are:");
        System.out.println("====================");
        for (String grade : grades) {
            System.out.println("Grade: " + grade);
        }

        grades.removeFirst();
        grades.removeLast();

        System.out.println("New values are:");
        System.out.println("====================");
        for (String grade : grades) {
            System.out.println("Grade: " + grade);
        }
    }
}

This program print out the following result:

Original values are:
====================
Grade: A
Grade: B
Grade: C
Grade: D
Grade: E
Grade: F
New values are:
====================
Grade: B
Grade: C
Grade: D
Grade: E

How do I retrieve particular object from LinkedList?

The example show you how to retrieve particular object from LinkedList using the indexOf() method.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;

public class LinkedListIndexOf {
    public static void main(String[] args) {
        List<String> names = new LinkedList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Carol");
        names.add("Mallory");

        // Search for Carol using the indexOf method. This method
        // returns the index of the object when found. If not found
        // -1 will be returned.
        int index = names.indexOf("Carol");
        System.out.println("Index = " + index);

        // We can check to see if the index returned is in the range
        // of the LinkedList element size.
        if (index > 0 && index < names.size()) {
            String name = names.get(index);
            System.out.println("Name  = " + name);
        }
    }
}

The program prints the following output:

Index = 2
Name  = Carol

How do I iterate LinkedList elements using ListIterator?

This example show you how to use the ListIterator interface to iterate elements of a LinkedList.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIterator {
    public static void main(String[] args) {
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        // Retrieves object from LinkedList using the ListIterator
        // interface.
        ListIterator<String> iterator = grades.listIterator();
        while (iterator.hasNext()) {
            System.out.println("Grades: " + iterator.next());
            System.out.println("  hasPrevious  : " + iterator.hasPrevious());
            System.out.println("  hasNext      : " + iterator.hasNext());
            System.out.println("  previousIndex: " + iterator.previousIndex());
            System.out.println("  nextIndex    : " + iterator.nextIndex());
        }
    }
}

The program will print the following output:

Grades: A
  hasPrevious  : true
  hasNext      : true
  previousIndex: 0
  nextIndex    : 1
Grades: B
  hasPrevious  : true
  hasNext      : true
  previousIndex: 1
  nextIndex    : 2
Grades: C
  hasPrevious  : true
  hasNext      : true
  previousIndex: 2
  nextIndex    : 3
Grades: D
  hasPrevious  : true
  hasNext      : true
  previousIndex: 3
  nextIndex    : 4
Grades: E
  hasPrevious  : true
  hasNext      : false
  previousIndex: 4
  nextIndex    : 5

How do I iterate LinkedList elements using Iterator?

This example show you how to use the Iterator interface to iterate elements of a LinkedList.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;

public class LinkedListIterator {
    public static void main(String[] args) {
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        // Iterates elements in LinkedList using Iterator.
        Iterator<String> iterator = grades.iterator();
        while (iterator.hasNext()) {
            System.out.println("Grade: " + iterator.next());
        }
    }
}

This program prints the following output:

Grade: A
Grade: B
Grade: C
Grade: D
Grade: E