Let's learn java programming language with easy steps

Monday, 7 August 2017

Difference Between ArrayList and LinkedList


ArrayList vs LinkedList

ArrayList vs LinkedList

Here we will learn in detail what is the difference between ArrayList and LinkedList in java programming language. This is the very important topic for the java interview.

So let's start with it.

There are many differences between array list and linked list class in java collection framework.

Java ArrayList

  • ArrayList class internally uses the dynamic array to store the elements.
  • ArrayList class provides slow manipulation because it uses internally array. If any element deletes or removes from the list or array, all the bits are shifted in memory.
  • ArrayList can work as the list only because it implements List interface only.
  • ArrayList is better for storing and accessing data or elements.

Now understand array list with an example.

Java ArrayList Example

This is the simple example of array list where we will insert elements and traverse these elements.

import java.util.*;
public class Sample
{
public static void main(String args[])
{
//Declaring ArrayList
List<String> l = new ArrayList<String>();
l.add("Apple");//inserting elements
l.add("Orange");
l.add("Mango");
l.add("Banana");
l.add("Guava");

//Traverse the elements by using Iterator interface
Iterator i = l.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output: Apple
            Orange
            Mango
            Banana
            Guava

Now moving to the next class which is LinkedList class.


Java LinkedList

  • LinkedList class internally uses the doubly linked list in java to store the elements.
  • LinkedList class provides faster manipulation because it uses doubly linked list internally so no bit shifting required in memory.
  • LinkedList can work as list and queue because it implements list interface and deque interfaces.
  • LinkedList is better for manipulation data i.e inserting, deleting an elements.

Now understand linked list with an example.



Java LinkedList Example

This is the simple example of LinkedList where we will insert an element and then traverse these elements one-by-one.

import java.util.*;
public class Demo
{
public static void main(String args[])
{
//Declaring the LinkedList
LinkedList<String> ll = new LinkedList<String>();
ll.add("Red");
ll.add("Red");
ll.add("Black")';
ll.add("Blue");
ll.add("Pink");
ll.add("Green");

//Traversing elements by using Iterator interface
Iterator i = ll.iterator();
while(i.hasNext())
{
System.out.println(i.next())
}
}
}

Output: Red
             Red
             Black
             Blue
             Pink
             Green


Similarities Between ArrayList Class and LinkedList Class

In the above paragraphs, we learned some differences between these two classes but there are some similarities between array list and linked list.

  1. Both classes implement List interface.
  2. Both classes maintain insertion order of an element.
  3. Both are non-synchronized.
  4. The iterator and list iterator returned by array list and linked list class are fail-fast.
So there are many java differences which are mostly asked in java interview e.g What is the difference between ArrayList and Vector in java and difference between HashSet and LinkedHashSet, etc.


ArrayList vs LinkedList Performance

ArrayList : By the help of array list we can perform fast searching. It is good for fast searching operation. But linked list is not good for fast searching operation in the list. But array list is not good for more insert or remove operations.

LinkedList : Linked list is not good for fast searching but it is good for more insert or deletes operations.





Share:

0 comments:

Post a Comment

Facebook Page Likes

Popular Posts

Translate