
How to use java collection-linked list ?

Example:
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class LinkedListExample{
public static void main(String [] args){
List list = new LinkedList();
list.add("Apple");
list.add("Mango");
list.add("Pine");
list.add("24");
list.add(24.34);
list.add('A');
System.out.println("Size :" + list.size());
list.remove("Pine");
Iterator it = list.iterator();
while(it.hasNext())
System.out.println(it.next());
System.out.println("Size :" + list.size());
}
}
Output:
Size :6 Apple Mango 24 24.34 A Size :5
Description:-In the above code, we have implemented List by its subclass LinkedList. Using the add() method, we have added elements to LinkedList. Then we have removed one element from the LinkedList by using the method remove(). The iterator method then iterates through the list elements and display the remaining elements.