linkedlist

Linked List Class Java with Programming Example

linkedlistIn this chapter you will learn:

  1. What is LinkedList Class in Java?
  2. Methods of Linked List Class
  3. Programming Example
 What is Linked List Class in Java?

Java LinkedList class uses doubly linked list structure to store element in the list. It extends the abstract class and implements List and Deque Interface. Java LinkedList class can contains null value or duplicate value. LinkedList class doesn’t support synchronization. It means if more than one thread is accessing Linked List simultaneously and one thread makes some changes the Linked List must be externally synchronized. Manipulation of Linked List is faster than ArrayList because no shifting needs to be occurred.

Initialization

You can define LinkedList class as follow. You must include java.util.*; in your program.

LinkedList list=new LinkedList();

Constructors of LinkedList class

Constructors Descriptions
LinkedList() This constructor builds an empty linked list.
LnkedList(Collection c) This constructor builds a linked list that is initialized with the elements of the collection c.

Methods of ArrayList Class

Methods Descriptions
boolean add(E e) Append the element at the end of the list.
void add(int index, E element) Insert the element at the specified position in the list.
boolean addAll(
Collection<? extends E> c)
Append entire element from one collection to the end of the list. The orders are defined as the iterator returns value.
boolean addAll(
int index, Collection<? extends E> c)
Insert entire elements from one collection to list at the specified postion.
void clear() Clear the list by removing all the items.
Object clone() Make a clone of arraylist.
boolean contains(Object o) returns true if arraylist contains specified items.
void ensureCapacity(int minCapacity) Increase the capacity of arraylist.
E get(int index) Returns the element at the specified index position in the list.
int indexOf(Object o) Returns the index of first occurrence of element in the list. If item not found returns -1.
boolean isEmpty() Returns true if list is empty.
Iterator iterator() Returns an iterator over the elements in this list in proper sequence.
int lastIndexOf(Object o) Returns the index of last occurrence of element in the list. If item not found returns -1.
ListIterator<E> listIterator() Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index) Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index) Removes the element at the specified position in this list.
boolean remove(Object o) Removes the first occurrence of the specified element from this list, if it is present.
boolean removeAll(Collection<?> c) Removes from this list all of its elements that are contained in the specified collection.
protected void removeRange(
int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
boolean retainAll(Collection<?> c) Retains only the elements in this list that are contained in the specified collection.
E set(int index, E element) Replaces the element at the specified position in this list with the specified element.
int size() Returns the number of elements in this list.
List subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element).
<T> T[] toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
void trimToSize() Trims the capacity of this ArrayList instance to be the list’s current size.

 

Programming Example

import java.util.*;

class Linked_List
{
  public static void main(String[] args)
  {
    LinkedList<String> list=new LinkedList<String>();
    list.add("Computer");
    list.add("Mobile");
    list.add("Tablet");
    list.add("iPhone");
    
    //Adding an item at the begining of the list
    list.addFirst("iPad");
    System.out.println(list);
    
    //Adding an item at the last position of the list
    list.addLast("Laptop");
    System.out.println(list);
   
    //Making clone
    Object obj=new Object();
    obj=list.clone();
    System.out.println("Clone : "+obj);
    
    //Remove item at index position 2
    System.out.println("Items Removed : "+list.remove(2));
    
    //Printing Using Iterator
    Iterator<String> itr=list.iterator();
    while(itr.hasNext())
    {
      System.out.print(itr.next()+" , ");
    }
  }
}

Output

D:\JavaProgram>javac Linked_List.java

D:\JavaProgram>java Linked_List
[iPad, Computer, Mobile, Tablet, iPhone]
[iPad, Computer, Mobile, Tablet, iPhone, Laptop]
Clone : [iPad, Computer, Mobile, Tablet, iPhone, Laptop]
Items Removed : Mobile
iPad , Computer , Tablet , iPhone , Laptop ,
_

Summary

In this chapter you have learned what is LInkedList class and their methods in Java with complete programming example. In the next chapter you will learn about Vector Class in Java.


Leave a Reply

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