LinkedList Class in Collection Framework

java.util.LinkedList;
public class LinkedList  extends AbstractSequentialList  implements List, Cloneable, Serializable
LinkedList class implementation of the List interface. Implements all optional list operations, and permits all elements (including null). A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another.
LinkedList class features:
  1. It is used to store an “insertion ordered” group of elements.
  2. It can contain duplicate elements.
  3. It is unsynchronized.
  4. It cannot access random.
  5. LinkedList manupulation is fast because it need not shifting.
  6. In LinkedList add element first and last by using given method
  7. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).
  8. All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
To prevent unsynchronized access to the list:
List list = Collections.synchronizedList(new LinkedList(…));
Constructor of LinkedList
  1. LinkedList()
    It is used to create  an empty list.
  2. LinkedList(Collection c)
              it is used to create a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Method of LinkedList class
  1. void add(int index, Object element)
             It is used to insert the specified element at the specified position in this list (optional operation).
  2. boolean add(Object o)
             It is used to append the specified element to the end of this list (optional operation).
  3. boolean addAll(Collection c)
              It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
  4. boolean addAll(int index, Collection c)
               It is used to insert all of the elements in the specified collection into this list at the specified position (optional operation).
  5. void  addFirst(Object o)
                 Inserts the given element at the beginning of this list.
  6. void  addLast(Object o)
                 Appends the given element to the end of this list.
  7. void clear()
              Removes all of the elements from this list (optional operation).
  8. boolean contains(Object o)
              Returns true if this list contains the specified element.
  9. boolean containsAll(Collection c)
              Returns true if this list contains all of the elements of the specified collection.
  10. Object get(int index)
              Returns the element at the specified position in this list.
  11. Object getFirst()
              Returns the first element in this list.
  12. Object getLast()
              Returns the last element in this list.
  13. int indexOf(Object o)
              Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
  14. int lastIndexOf(Object o)
              Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
  15. ListIterator listIterator (int index)
              Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
  16. Object remove(int index)
              Removes the element at the specified position in this list (optional operation).
  17. boolean remove(Object o)
              Removes the first occurrence in this list of the specified element (optional operation).
  18. Object removeFirst()
                Removes and returns the first element from this list.
  19. Object removeLast()
              Removes and returns the last element from this list.
  20. Object set(int index, Object element)
              Replaces the element at the specified position in this list with the specified element (optional operation).
  21. int size()
              Returns the number of elements in this list.
  22. Object[] toArray()
              Returns an array containing all of the elements in this list in proper sequence.
  23. Object[] toArray(Object[] a)
              Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
Example of LinkedList class
import java.util.LinkedList; import java.util.Iterator; public class SimpleLinkedList{ public static void main(String[] args) { // LinkedList Creation LinkedList ll = new LinkedList(); // Adding elements to the LinkedList ll.add("101"); ll.addFirst("100"); ll.add("103"); ll.add("105"); ll.add("106"); ll.addLast("109"); ll.add(3,"104"); // Display content of linkedList System.out.println("Content of Linked List " + ll); // Display content of linkedList using iterator Iterator it=ll.iterator(); System.out.println("Content of Linked List using iterator : "); while(it.hasNext()) { System.out.println(it.next()); } } }
Output

Content of Linked List [100, 101, 103, 104, 105, 106, 109]
Content of Linked List using iterator :
100
101
103
104
105
106
109

Download this example
 



 

Latest Tutorial


Most Visited Tutorial