Skip to content
Related Articles

Related Articles

Difference between Arrays and Collection in Java

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 29 Dec, 2021

An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. On the other hand, any group of individual objects which are represented as a single unit is known as the collection of the objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface in it.

The most essential thing while dealing Collection is to have super strong grasp of Collection framework which in one go is depicted via below image as follows:

Example

Java




// Java Program to Illustrate Difference
// Between Arrays and Collection
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Arrays
        String[] gfg
            = new String[] { "G", "E", "E", "K", "S" };
 
        // Trying printing the above array
        System.out.print(gfg);
 
        // New Line
        System.out.println();
 
        // Collection
        // Let us arbitarly create an empty ArrayList
        // of string type
        ArrayList<String> al = new ArrayList<String>();
 
        // Adding elements to above List
        // using add() method
        al.add("g");
        al.add("e");
        al.add("e");
        al.add("k");
        al.add("s");
 
        // Printing all elements of Collection (ArrayList)
        System.out.println(al);
    }
}

Output

[Ljava.lang.String;@3d075dc0
[g, e, e, k, s]

Now after having understanding of Arrays and Collection, let us now tabulate differences between them which is as follows:

ArraysCollection
Arrays are fixed in size that is once we create an array we can not increased or decreased based on our requirement.Collection are growable in nature that is based on our requirement. We can increase or decrease of size.
With respect to memory Arrays are not recommended to use.With respect to memory collection are recommended to use.
With respect to performance Arrays are recommended to use.With respect to performance collection are not recommended to use.
Arrays can hold only homogeneous data types elements.Collection can hold both homogeneous and and heterogeneous elements.
There is no underlying data structure for arrays and hence ready made  method support is not available.Every collection class is implemented based on some standard data structure and hence for every requirement ready made method support is available being a performance. we can use these method directly and We are not responsible to implement these methods.
Arrays can hold both object and primitive.Collection can hold only object types but not primitive datatypes such as int, long, short, etc. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!