LinkedList class

In this chapter you will learn:

  1. LinkedList class

LinkedList class

The LinkedList class extends AbstractSequentialList and implements the List, Deque, and Queue interfaces. It provides a linked-list data structure. LinkedList is a generic class that has this declaration:

class LinkedList<E>

E specifies the type of objects that the list will hold.

A demonstration of a linked list of nodes

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/*from  j av  a 2  s  . c  o m*/
public class Main {
  public static void main(String[] args) {
    List<String> ls = new LinkedList<String>();
    String[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    for (String weekDay : weekDays){
      ls.add(weekDay);
    }      
    dump("ls:", ls);
    ls.add(1, "A");
    ls.add(3, "B");
    ls.add(5, "C");
    dump("ls:", ls);
    ListIterator<String> li = ls.listIterator(ls.size());
    while (li.hasPrevious()){
      System.out.print(li.previous() + " ");
    } 
  }

  static void dump(String title, List<String> ls) {
    System.out.print(title + " ");
    for (String s : ls){
      System.out.print(s + " ");
    }      
    System.out.println();
  }
}

Because LinkedList implements the Deque interface, you have access to the methods defined by Deque.

For example,

  • To add elements to the start of a list you can use addFirst( ) or offerFirst( ).
  • To add elements to the end of the list, use addLast( ) or offerLast( ).
  • To obtain the first element, you can use getFirst( ) or peekFirst( ).
  • To obtain the last element, use getLast( ) or peekLast( ).
  • To remove the first element, use removeFirst( ) or pollFirst( ).
  • To remove the last element, use removeLast( ) or pollLast( ).

Next chapter...

What you will learn in the next chapter:

  1. Create LinkedList
Home » Java Tutorial » List

List

    List interface
    List add/insert elements
    List clear/remove elements
    List search
    List element get and set
    List and its Iterator
    List size, empty
    List conversion, to array
    List to sublist
    List comparison
    List filling
    List reversing
    List rotating and shuffling
    List sorting
    List element swap
    List element replacing
    List copy
    List binary search

ArrayList

    ArrayList
    ArrayList Creation
    ArrayList add/insert
    ArrayList get/set element
    ArrayList clear/remove
    ArrayList search
    ArrayList copy and shallow copy
    ArrayList size, trim to size and capacity
    ArrayList to array

LinkedList

    LinkedList class
    LinkedList creation
    LinkedList add/insert elements
    LinkedList get elements
    LinkedList search
    LinkeList replace/set elements
    LinkedList remove element
    LinkedList copy
    LinkedList iterator
    LinkedList peek element
    LinkedList pop/push element
    LinkedList conversion

List Utilities

    List filling
    List reversing
    List rotating and shuffling
    List sorting
    List element swap
    List element replacing
    List copy
    List binary search