• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

LinkedList in Java

January 21, 2014 //  by Krishna Srinivasan//  Leave a Comment

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

package javabeat.net.core;

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()) {
			System.out.println(iterator.next());
		}
	}
}

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)

Category: JavaTag: Java Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « JSP API
Next Post: HashSet in Java »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact