Tuesday, 20 December 2016

Difference : List Vs Set

List and Set differs based on the following parameters.

1.       Duplicate Object
List allows duplicate elements to be stored in it.
Set doesn’t allow duplicate element. All the element needs to be unique.

2.       Order Maintenance
List generally maintains the insertion order.
In set maintenance of insertion order depends on the implementing class. (HashSet-> order not guaranteed, LinkedHashSet-> Order is maintained)

3.       Null value
In List, one can store unlimited null values
In Set, only one null value is permitted.

4.       Implementations
ArrayList class, LinkedList class and Vector class implements List interface.
HashSet class, LinkedHashSet class and TreeSet class implements Set interface.

5.       Traversal
You can use both ListIterator and Iterator to traverse elements in List.
You cannot use ListIterator to iterate Set. To traverse Set use Iterator.

Example

package collection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ListandSet {

      public static void main(String[] args) {
            List<String> arrayList = new ArrayList<String>();
            arrayList.add("A");
            arrayList.add("B");
            arrayList.add("A");
            arrayList.add("C");
            arrayList.add("D");
            System.out.println("List elements:"+ arrayList);
           
            Set<String> set = new HashSet<String>();
            set.add("A");
            set.add("B");
            set.add("A");
            set.add("C");
            set.add("D");
            System.out.println("Set elements:"+ set);

      }


}

Output:
List elements:[A, B, A, C, D]
Set elements:[A, B, C, D]

Saturday, 17 December 2016

Difference : ArrayList Vs HashMap


ArrayList and HashMap differ based on following parameters:

1.      Approach
ArrayList stores only the value of the element.
HashMap applies different mechanism as it stores value of the element along with a unique keyassociated to the stored value.

2.      Memory requirement
As ArrayList only stores the value of the element so it requires less memory space in comparision to HashMap.
On other hand HashMap needs to store a unique key for each value so memory requirement increases.

3.      Implementation
ArrayList implements List interface.
HashMap implements Map interface.

4.      Order maintainence
ArrayList ensures that the insertion order is maintained.
HashMap doesn’t guarantee of maintaining insertion order (if you care about order, use LinkedHashMap).
*In case where insertion order is maintained we get the elements returned in same order in which they were inserted while retrieving them.

5.      Null value
Any number of null values can be stored in ArrayList.
Only one null key is allowed in HashMap. But you can store any number of null values.

6.      Duplicate value
ArrayList accepts duplicate values while storing.
HashMap doesn’t accept duplicate Key. But duplicate values are allowed.

7.      Retrieve element
In ArrayList elements can be retrieved specifying index of the element.

Tuesday, 13 December 2016

Difference: ArrayList Vs Array

ArrayList and Array differ based on following parameters:

1.       Nature
Array is static in nature. Its length is predefined and thus array cannot grow or shrink as per need. If it gets full then we cannot add more element to it.

ArrayList is dynamic in nature thus grows or shrink automatically as per the requirement.


2.       Memory Consumption
If element is removed from Array, the memory consumption doesn’t decrease as array doesn’t shrink automatically. It remains fixed even if element is removed thus holding memory without providing any proper utilization.

If element is removed from ArrayList, it automatically shrinks thus saving programmer from the issue of wasting memory.


3.       Provision of predefined methods
ArrayList provides lots of predefined methods which helps in making programmer task easier while implementing code thus gaining advantage over Array.

      4.       Example


Array Example:

package array;

public class ArrayExample {

      public static void main(String[] args) {
            int array[] = new int[5];
           
            for(int i=0;i<=5;i++){
                  array[i]=i*i;
                  System.out.println(i+" index contains "+ array[i]);
            }

      }

}


OUTPUT:
0 index contains 0
1 index contains 1
2 index contains 4
3 index contains 9
4 index contains 16
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at array.ArrayExample.main(ArrayExample.java:9)


Why is the exception thrown here?

Reason: We have declared array which has capacity to store 5 elements at max. So it stores 5 elements without any complaint(from index 0 to 4). But when we are trying to store value (5*5) at index array[5] we are sure to get ArrayIndexOutOfBoundsException: 5
To store one more element we need to have array of size 6. So here you have to change size like this:

int array[] = new int[6];

Now save the program and run again to see 25 getting stored at index 5.


ArrayList Example

package javaRadarArrayList;

import java.util.ArrayList;

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }

}

OUTPUT:
[Java, Spring, Hibernate, EJB]

In ArrayList we don't need to declare size. It automatically allocates a default size first. And later if required arraylist automatically re-sizes itself.

Friday, 9 December 2016

Vector in java


Vector belongs to java.util package. Vector is a class that extends AbstractList and implements List interface, RandomAccess interface, Serializable interface and Cloneable interface. It also implements grow able array of objects. The elements in vector can be accessed using integer index just as we do in array.

Vector is synchronized and thus should be preferred over ArrayList if thread-safety is concern of the developer. 
Due to synchronized nature, it does not allow multiple threads to access and modify itself concurrently. If one thread has access to the vector then other thread wanting to access and operate on it must wait for its turn until the vector is released by the thread in action.

On other hand, this synchronized nature affects Vector’s performanceSo ArrayList is preferred over Vector if performance is to be considering factor.




Difference

·         Vector vs ArrayList


Now let’s look at the ways in which you can create vector in java. 

·         Vector myVector = new Vector<E>();
·         Vector myVector = new Vector<E>(Collection c);
·         Vector myVector = new Vector<E>( (int initialCapacity);
·         Vector myVector = new Vector<E>(int initialCapacityint capacityIncrement);


Now let’s look at different ways in which you can use the Vector that you created.

Vector operations
·         Sort the elements in vector
·         Search element in vector
·         Copy element of vector to another vector
·         Remove element from vector
·         Remove all elements from the vector
·         Remove element from a particular index in vector

Traverse Vector
·         Traverse vector using Iterator
·         Traverse vector using ListIterator
·         Traverse Vector using Enumeration

Convert
·         Vector to ArrayList
·         Vector to Array
·         Vector to LinkedList

Wednesday, 7 December 2016

Difference : ArrayList Vs Vector


Given below are the parameters on which ArrayList and Vector differs from each other :

1.      Synchronization :
ArrayList is non-synchronized. It is for same reason that multiple threads can operate on ArrayList at the same time.

Vector is synchronized. It is for same reason that two threads cannot get hold of a vector at the same time. If one thread is executing an operation on vector, other thread needs to wait till the executing thread releases it.

2.      Performance :
ArrayList is better performance-wise. As you know by now, ArrayList is non-synchronized so multiple threads can work on ArrayList at the same time.

Vector lags performance wise when compared to ArrayList. Vector is thread-safe. When a thread is performing any operation on Vector then it gets a lock. If other thread wants to perform operation on same Vector then it needs to wait until the Vector is unlocked (released) by thread in action.

3.      Size :
ArrayList grows by half of its size, if required.
Vector grows by double of its size, if required.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.