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

Friday, 4 August 2017

Difference Between Array and Collection In Java

Difference Between Array and Collection

Difference Between Array and Collection

Now here we are going to discuss what is the difference between array and collection in java. 

So there are many differences between these two concepts i.e array and collection.

Arrays vs collections in java is also a very important java interview question which is mostly asked.

So without wasting time, let's start


Array

  • Array is a collection of similar types(i.e similar data types) of elements with the contiguous memory location.
  • Array is an object in java.
  • Java Array is fixed in size i.e the size of the array cannot be increased or decreased once when it is declared.
  • We can store both primitive types and objects in an array.
  • Array consumes more memory.
  • Performance point of view arrays faster than a collection.
  • There is no predefined method for the array available.
  • Array can hold only homogeneous elements.


Simple Java Array Example

public class Demo
{
public static void main(String args[])
{
//declaration, instantiation, initialization of an array
int a[] = {5, 7,3,8,1,9,6,4,2};
for(int i = 0; i<a.length; i++)
{
System.out.println(a[i]);
}
}
}

Output : 5
              7
              3
              8
              1
              9
              6
              4
              2


Collection

  • Collection allows to a group of an object to be treated as a single unit.
  • Collection is a framework or interface in java.
  • Collection is dynamic in nature i.e collection can grow(increase or decrease) their size as needed.
  • Collection can only store objects. It doesn't store primitive data types.
  • Collection consumes less memory in comparison of arrays.
  • Performance point of views collection is not faster than an array.
  • There are predefined methods are available for collection.
  • Collection can hold both homogeneous and heterogeneous elements.


There are many classes which implement the collection interface like ArrayList class, LinkedList class, HashSet class, and LinkedHashSet class etc. But here we will take an example of ArrayList class.



Java ArrayList Example - Collection

import java.util.*;
public class Test
{
public static void main(String args[])
{
//declaring ArrayList 
ArrayList<String> al = new ArrayList<String>();
al.add("Seema");//insert elements
al.add("Baby");
al.add("Sid");
al.add("Tara");
al.add("Seema");
al.add("Gudia");

//traverse the elements
Iterator i = al.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output : Seema
              Baby
              Sid
              Tara
              Seema
              Gudia


Similarities Between Array and Collection

According to my point of view, The similarities between array and collection are that both can work on the index basis. For example

In an array, we can access any elements at any index position. Java array provides us random access by using index position. The index positions start with 0 index.

In the collection, we can get any element by using index number. There are some classes of the collection framework which works at index basis.


Which is Better Array or Collection

This is very difficult to say which is better array or collection but performance wise array is better than a collection. And with respect to memory collection is better than array. So there are some places array is better and some place collection is better.

Read More:

Difference Between Abstract Class and Interface in Java.
Difference Between Class and Interface in Java.
Difference Between Constructor and Method in Java.
Difference Between String and StringBuffer.
Difference Between C++ and Java.
Difference Between ArrayList and Vector in Java.

Share:

5 comments:

  1. Thanks for sharing this complete information about difference between array and collection

    ReplyDelete
  2. Thanks for sharing this informative blog java training in OMR

    ReplyDelete
  3. Informative Blog! I really liked your blog so much, thanks for posting the educational blog on Java. I have bookmarked to see new information on your blog. Keep on posting blog like this.
    Refer my course Details: https://careerpluz.in/java-training-madurai
    java training in Madurai | java programming course | java j2ee training | java placement assurance | advanced java programming

    ReplyDelete
  4. Informative Blog! I really liked your blog so much, thanks for posting the educational blog on Java. I have bookmarked to see new information on your blog. Keep on posting blog like this.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate