Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 19 July 2017

Difference Between ArrayList and Vector

ArrayList vs Vector - Java Collection Framework

Difference Between ArrayList and Vector

Now here we will learn what is the differences between ArrayList and Vector class in java. " what is the differences between ArrayList and Vector in java " this is mostly asked question in any java interviews.

There are many differences between ArrayList and Vector in java collection framework. Let's see

First starts with ArrayList class.

ArrayList Class

  • ArrayList class are non-synchronized i.e multiple thread can access simultaneously. In other words, ArrayList class are not Thread safe.
  • ArrayList class is not a legacy class i.e old class in java collection.
  • ArrayList class is faster than vector class because it is non-synchronized.
  • The size increment of ArrayList class is 50% i.e half as needed.
  • ArrayList class uses Iterator interface to traverse the elements.

Java ArrayList Example

This is the simple example of java ArrayList class where we will insert some elements in this list and after inserting elements we will traverse all the elements from the array list.

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample
{
public static void main(String args[])
{
//declaration of ArrayList class
ArrayList<String> al = new ArrayList<String>();
al.add("java");//insert elements
al.add("python");
al.add("c++");
al.add("c++");
al.add("html");
al.add("css");
al.add("java");

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

output : java
              python
              c++
              c++
              html
              css
              java

In the above example, as we know ArrayList class can allow duplicate elements and maintains insertion order of an elements.

Now, move to Vector class in java collection.

Vector Class

  • Vector class is a synchronized class i.e multiple thread cannot access simultaneously, but one-by-one they can. In other words, Vector class is a Thread safe class.
  • Vector class is slower than ArrayList class because Vector class is synchronized(thread safe) class.
  • The size increment of Vector class is 100% i.e double as needed.
  • We can use both Iterator interface and Enumeration interface to traverse the elements of vector class.



Java Vector Example

In this example, we will use some method of Vector class for inserting an elements in Vector and use Enumeration interface to traverse the elements.

import java.util.*;
public class VectorExample
{
public static void main(String args[])
{
//declare Vector class
Vector<String> v = new Vector<String>();
v.add("rahul");//collection's method
v.add("ravi");
v.add("mohit");
v.addElement("rahul");//vector's method
v.addElement("love");
v.addElement("kush");

//traversing an elements by using Enumeration interface
Enumeration e = v.elements();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
}
}

output : rahul
              ravi
              mohit
              rahul
              love
              kush

In the above example, Vector class allows duplicate elements and maintains insertion order of an elements.

Similarities Between ArrayList and Vector Class

There are some similarities between ArrayList and Vector class in java.

(1) ArrayList and Vector both class implements List interface.
(2) ArrayList and Vector both allows duplicate elements.
(3) ArrayList and Vector both maintain insertion order of an element.
(4) By using Iterator interface we can traverse all the elements of ArrayList and Vector.
(5) Both class ArrayList and Vector class belong to the java.util package.

Visit : Java Collection Interview Questions.
Share:

1 comment:

  1. nice post about arraylist and vector in java.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate