Home » Java programming language

Compare Array and Collection in Java

In this article, we are going to learn how to compare Array and Collection java with examples?
Submitted by Preeti Jain, on December 25, 2017

Difference between Array and Collections in Java

Arrays:

1) Arrays are fixed in size (i.e. we can’t increase or decrease size at runtime).

Example

class ArrayClass
{
	public static void main(String[] args){
		int[] a = new int[10];
		System.out.println(a[0]);
	}
}

Output (Description)

Memory is created for 10 elements and all elements initially initialized with 0 (i.e. new keyword created object) and a[0] means printing first element value i.e 0.

E:\javasource>java ArrayClass
0 

2) In case of memory point of view arrays concept is not recommended to use(i.e. arrays size are fixed if we use memory for elements less than arrays size so memory will be wasted).

3) In case of performance point of view arrays concept is recommended to use (i.e. we know the size of arrays in advance or at compile time so no overhead at runtime that’s why it takes less time).

4) Arrays can hold homogeneous data elements (i.e. Array elements are of same type).

Example

class ArrayClass
{
	public static void main(String[] args){
		int[] a = new int[10];
		a[0]	= new boolean[10];
		System.out.println(a[0]);
	}
}

Output (Description)

a is of int[] type array, so it can hold only int[] elements but what we are doing here a[0] = new boolean[10] means assigning Boolean value in int type array so we will get compiletime error if we write a[0]= new int[10] instead of a[0]= new boolean[10] then no error .

E:\javasource>javac ArrayClass.java
ArrayClass.java:8: incompatible types
found   : boolean[]
required: int
                        a[0]    = new boolean[10];
                                  ^
1 error

5) Arrays don’t provide readymade method support that’s why we can call as arrays is not underlying data structure.

6) Arrays are capable to hold both primitives (byte, short, int, long etc.) and Objects (Wrapper Classes, String, StringBuffer or Any user defined classes).

Example 1: For Object Type

class ArrayClass
{
	public static void main(String[] args){
		Integer[] a = new Integer[10];
		System.out.println(a[0]);
	}
}

Output (Description)

Memory is created for 10 elements and all elements initially initialized with null (i.e. new keyword created object and object type element contains null by default if under not defined) and a[0] means printing first element value i.e null.

E:\javasource>java ArrayClass
null

Example 2: For Primitives Type

class ArrayClass
{
	public static void main(String[] args){
		int[] a = new int[10];
		System.out.println(a[0]);
	}
}

Output (Description)

Memory is created for 10 elements and all elements initially initialized with 0 (i.e. new keyword created object) and a[0] means printing first element value i.e 0.

E:\javasource>java ArrayClass
0 

Collections:

1) Collections are growable in nature (i.e. we can increase or decrease size at runtime).

Example:

import java.util.*;

class CollectionsClass
{
	public static void main(String[] args){
		ArrayList al = new ArrayList(2);
		al.add(10);
		al.add(20);
		System.out.println(al);
		al.add(30);
		System.out.println(al);
	}
}

Output (Description)

Initially, ArrayList size is 2 then 2 element will be added then third element came so new ArrayList will be created so new capacity = current capacity*3/2 +1

E:\javasource>java CollectionsClass
[10, 20]
[10, 20, 30]

2) In case of memory point of view collections concept is recommended to use (i.e. collections size are not fixed memory will be allocated according to collections elements size).

3) In case of performance point of view collections concept is not recommended to use (i.e. we don’t know the size of collections in advance or at compile time let suppose initially memory is allocated for 10 elements As 11th element then again new memory will be allocated and all elements will be copied in new memory).

4) Collections can hold both homogeneous and heterogeneous data elements (i.e. Collections elements may be of different type).

Example:

import java.util.*;

class CollectionsClass
{
	public static void main(String[] args){
		ArrayList al = new ArrayList(10);
		al.add("A");
		al.add("true");
		System.out.println(al);
	}
}

Output (Description)

ArrayList al object can hold both homogeneous and heterogeneous element like A is String type and true is Boolean type both are of different type (heterogeneous).

E:\javasource>java CollectionsClass
[10, true]

5) Collections provide readymade method support that’s why we can call as collections is underlying data structure.

Example:

import java.util.*;

class CollectionsClass
{
	public static void main(String[] args){
		ArrayList al = new ArrayList(10);
		al.add("A");
		al.add("true");
		al.remove(1);
		System.out.println(al.size());
	}
}

Output (Description)

add(), remove(), size() etc. are the readymade methods .

E:\javasource>java CollectionsClass
1

6) Collections are capable to hold only Objects (Wrapper Classes, String, StringBuffer or Any user defined classes) type. No such facility for primitives.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.