LinkedList in Java

LinkedList is not most commonly used class for storing the values. The LinkedList class extends AbstractSequentialList and implements the List interface. It stores the data using linked-list data structure. This class has two constructors,

  1. With empty parameters – LinkedList() and
  2. With Collection object as the parameter – LinkedList (Collection c)

Some of the important features of this class is:

  • It supports the duplicate elements insertion
  • Similar to ArrayList, it maintains the insertion order
  • LinkedList is not synchronized
  • It doesn’t support the random access for retrieving the values
  • It can be used as list, stack or queue.

One of the main drawback of using the linked list is that each element should have a pointer to the next and previous elements, It is extra overhead for each elements. This overhead is not with the ArrayList. The only time when LinkedList is better than ArrayList is that, when performing insertion or removal with the iterator which is more efficient than the ArrayList. In ArrayList every insertion or removal needs the copy of objects to new array.

LinkedList Example


import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListDemo {
	public static void main(String args[]) {
		LinkedList<String> list = new LinkedList<String>();
		list.add("Element 1");
		list.add("Element 2");
		list.add("Element 3");
		list.add("Element 4");
		Iterator<String> iterator = list.iterator();
		while (iterator.hasNext()) {

Methods in LinkedList

The following are the methods defined in the LinkedlIst class. If you want to read more about each methods, please refer documentation.

  1. void add(int index, Object element)
  2. boolean add(Object obj)
  3. boolean addAll(Collection c)
  4. boolean addAll(int index, Collection c)
  5. void addFirst(Object obj)
  6. void addLast(Object obj)
  7. void clear()
  8. Object clone()
  9. boolean contains(Object obj)
  10. Object get(int index)
  11. Object getFirst()
  12. Object getLast()
  13. int indexOf(Object obj)
  14. int lastIndexOf(Object obj)
  15. ListIterator listIterator(int index)
  16. Object remove(int index)
  17. boolean remove(Object obj)
  18. Object removeFirst()
  19. Object removeLast()
  20. Object set(int index, Object element)
  21. int size()
  22. Object[] toArray()
  23. Object[] toArray(Object[] a)

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!