Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaDifference between Arrays and Collection in Java

Difference between Arrays and Collection in Java

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:

  Arrays Collection
1 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.
2 Write in memory Arrays are not recommended to use. Write in  memory collection are recommended to use.
3 With respect to performance Arrays are recommended to use. With respect to performance collection are not recommended to use.
4 Arrays can hold only homogeneous data types elements. Collection can hold both homogeneous and heterogeneous elements.
5 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, ready-made method support is available for every requirement. As performance is crucial, we can use these methods directly, and we are not responsible for implementing them.
6 Arrays can hold both object and primitive data type . Collection can hold only object types but not primitive datatypes such as int, long, short, etc. 
RELATED ARTICLES

Most Popular

Recent Comments