Removing LinkedList Items - Java Collection Framework

Java examples for Collection Framework:LinkedList

Introduction

You can remove items with its index number or a reference to the object you want to remove on the remove method.

To remove item 3, use a statement like this:

Demo Code

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList<String> officers = new LinkedList<String>();

    // add the original officers
    officers.add("Blake");
    officers.add("Jack");
    officers.add("Tuttle");
    officers.add("book2s");
    officers.add("Pierce");
    officers.add("java2s.com");
    System.out.println(officers);

    officers.remove(3);//from   w  w  w . j a  v  a 2s . c o  m
    
    
    System.out.println(officers);

  }

}

And if you have a reference to the item you want to remove, use the remove method like this:

officers.remove(value);

To remove all the items from the list, use the clear method:

officers.clear();

Related Tutorials