Pages

Sunday, August 27, 2017

Introduction to Java Collection over Arrays

Arrays Vs Collections

 An array is an indexed collection of fixed number of homogeneous data elements.
 The main advantageous of array is we can represent multiple values by using single variable. So
 that readability of the code will be improved.

29. Collection over Arrays




Limitations of Arrays:

 

1)    Arrays are fixed in size. I.e. once we create an array there is no chance of increasing or
        decreasing size based on our requirement. Due to this, to use arrays concept compulsory we    
        should know the size in advance which may not possible always.

2)    Array can hold only homogeneous data type elements.
        E.g.

        Student[] s = new Student[10000];
        s[0] = new Student(); 
        s[1] = new Customer(); × CE: incompatible types found: Customer, required: Student.


       We can solve this problem by using Object type arrays.

       Object[] a = new Object{10000];
       a[0] = new Student();
       a[1] = new Customer();

3)    Arrays concept is not implemented based on some standard data structure & hence, ready-made 
       method support is not available. For every requirement, we have to write the code explicitly
       which increases complexity of programming.


Being a programmer we are responsible to use those methods and we are not responsible to implement those methods.

Differences between Arrays & Collections:




Arrays
Collection
      1.   Arrays are fixed in size. Once we create an array, we cannot increase or decrease the size based on our requirement.
      1.  Collections are growable in nature. I.e. based on our requirement we can increase or decrease the size.
      2. With respect to memory, arrays are not recommended to use.
      2. With respect to memory, collections are recommended to use.
      3. With respect to performance, arrays are recommended to use.
      3. With respect to performance, collections are not recommended to use.
      4. Arrays can hold only homogeneous data type elements.
      4. Collections can hold, both homogeneous and heterogeneous elements.
      5. There is no underlying data structure for arrays & hence ready method support is not available.
For every requirement, we have to write the code explicitly which increases complexity of programming.
      5. For every collection class is implemented based on some standard data structure & hence for every requirement ready-made method support is available. Being a programmer, we can use these methods directly and we are not responsible to implement those methods.
      6. Arrays can hold both primitives & Objects.
      6.  Collections can hold only object types but not primitives.


Basic Array Example:

 

package com.adeepdrive.array;

public class ArrayExample {
    public static void main(String[] args) {
        String[] names = new String[3];
        names[0] = "Shiva";
        names[1] = "Ardha";
        names[2] = "Nari";
        System.out.println("Printing array values ");
        for (int i = 0; i < names.length; i++) {
            System.out.println("Name " + (i + 1) + " is " + names[i]);
        }
    }
}

Output:

print array values
Name 1 is Shiva
Name 2 is Ardha
Name 3 is Nari

Basic Collection Example:

import java.util.ArrayList;
import java.util.List;

public class CollectionExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<String>();
        names.add("John");
        names.add("Rock");
        names.add("Cena");
        System.out.println("Iterating collection foreach loop.");
        for (String name : names) {
            System.out.println("name " + name);
        }
    }
} 

Output:

Iterating collection for-each loop.
name : John
name : Rock
name : Cena  

No comments:

Post a Comment

Please do not add any spam links in the comments section.