LinkedList - remove, get, contains and set methods program in java






LinkedList - remove, get, contains and set methods program in java.





import java.util.LinkedList;
import java.util.List;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class LinkedListExample {
   public static void main(String args[]) {
          List<String> linkedList = new LinkedList<String>();
          linkedList.add("ankit");
          linkedList.add("mittal");
          linkedList.add("javaMadeSoEasy");
          System.out.println("linkedList = "+linkedList);
         
          System.out.println("\n--------get element at 1st index i.e. mittal");
          System.out.println("element at 1st index = "+linkedList.get(1));
         
          System.out.println("\n--------contains element  ");
          System.out.println("linkedList.contains = "+linkedList.contains("mittal"));
         
          System.out.println("\n--------Remove element at 1st index i.e. mittal");
          linkedList.remove(1);  
          System.out.println("linkedList = "+linkedList);
          System.out.println("\n---------Remove ankit from list");
          linkedList.remove("ankit");
          System.out.println("linkedList = "+linkedList);
          System.out.println("\n---------Set newVal at 0th index");
          linkedList.set(0,"newVal"); //javaMadeSoEasy will be replace by newVal
          System.out.println("linkedList = "+linkedList);
         
         
         
         
         
   }
}
/*OUTPUT
linkedList = [ankit, mittal, javaMadeSoEasy]
--------get element at 1st index i.e. mittal
element at 1st index = mittal
--------contains element
linkedList.contains = true
--------Remove element at 1st index i.e. mittal
linkedList = [ankit, javaMadeSoEasy]
---------Remove ankit from list
linkedList = [javaMadeSoEasy]
---------Set newVal at 0th index
linkedList = [newVal]
*/


RELATED LINKS>

ArrayList - add, add element at specific index methods program


ArrayList - iterate using iterator, listIterator, Enumeration and enhanced for loop


ArrayList - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop


ArrayList - isEmpty, size and clear methods program


ArrayList - synchronizing using Collections.synchronizedList


ArrayList - making list unmodifiable using Collections.unmodifiableList


Vector - add, add element at specific index methods program


No comments:

Post a Comment