11:19 pm - Sunday February 19, 2017

Remove a data from LinkedList by its key V remove(Object key)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how  to Remove a data from LinkedList by its key using V remove(Object key) method.

Removes a single instance of the specified element from this collection, if it is present

 if this collection contains one or more such elements. Returns true if this collection

     contained the specified element.

Example

import java.util.LinkedList;

public class RemoveElements

{

   public static void main(String a[])

  {         

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

          arr.add("First");

          arr.add("Second");

          arr.add("Third");

          arr.add("Random");

          arr.add("four");

          arr.add("five");           

          arr.add("six");

          arr.add("seven");

          arr.add("eight");

          arr.add("nine");

          arr.add("eight");

          System.out.println(arr);

          System.out.println("Remove() method:"+arr.remove());

          System.out.println("After remove() method call:");

          System.out.println(arr);

          System.out.println("remove(index) method:"+arr.remove(2));

          System.out.println("After remove(index) method call:");

          System.out.println(arr);

          System.out.println("Remove(object) method:"+arr.remove("six"));

          System.out.println("After remove(object) method call:");

          System.out.println(arr);

          System.out.println("removeFirst() method:"+arr.removeFirst());

          System.out.println("After removeFirst() method call:");

          System.out.println(arr);

          System.out.println("removeFirstOccurrence() method:"

                              +arr.removeFirstOccurrence("eight"));

          System.out.println("After removeFirstOccurrence() method call:");

          System.out.println(arr);

          System.out.println("removeLast() method:"+arr.removeLast());

          System.out.println("After removeLast() method call:");

          System.out.println(arr);

          System.out.println("removeLastOccurrence() method:"

                              +arr.removeLastOccurrence("five"));

          System.out.println("After removeLastOccurrence() method call:");

          System.out.println(arr);

   }

}

Output

      [First, Second, Third, Random, four, five, six, seven, eight, nine, eight]

     Remov() method:First

     After remove() method call:

     [Second, Third, Random, four, five, six, seven, eight, nine, eight]

     remove(index) method:Random

     After remove(index) method call:

     [Second, Third, four, five, six, seven, eight, nine, eight]

     Remov(object) method:true

     After remove(object) method call:

     [Second, Third, four, five, seven, eight, nine, eight]

     removeFirst() method:Second

     After removeFirst() method call:

     [Third, four, five, seven, eight, nine, eight]

     removeFirstOccurrence() method:true

     After removeFirstOccurrence() method call:

     [Third, four, five, seven, nine, eight]

     removeLast() method:eight

     After removeLast() method call:

     [Third, four, five, seven, nine]

     removeLastOccurrence() method:true

     After removeLastOccurrence() method call:    [Third, four, seven, nine]

BACK

Filed in: Java Examples

Comments are closed.