Sunday, August 21, 2011

Array vs Collection

I do not understand why do we need both array and collection in Java ? I mean, aren't both of them the same - "a group of objects as a single unit" ? Why do we still need array (to exist) when the collection framework is so much better... giving us so many facilities to manipulate "a group of objects" ?

Arrays and Collections are complementary. Each have their own advantages and drawbacks. In some case, it is better to simply use arrays instead of Collections. For instance, say you want to store a sequence of integer primitives. If we only had collections, we would have to wrap each integer primitive in an java.lang.Integer. Imagine you have to store 2 millions (or more) ints. You would create lots of objects for nothing. Arrays further provide type safety. You know that an array declared as

Item[] item = new Item[21];

will only contain objects whose type is Item or a subclass thereof. You don't have that guarantee will Collections (yet, see JSR 14: Add Generic Types To The JavaTM Programming Language).     Collections are generic, that is, everything you get out of them will be an Object that you will have to cast to the correct type. Frankly, choosing between Collections and Arrays is not always a piece of cake, your application may suffer from bad choices. Check out Java tutorial: What are the Benefits of a Collections Framework? to see what advantages the Collections framework provide.

public class ArrayTest {

    static int size = 10000;
    static long temp;
    static long start;
    static long finish;
    
    int[] iArr = new int[size];
    ArrayList iArrayList = new ArrayList();
    LinkedList iLinkedList = new LinkedList();
    
    Integer item =  new Integer(1);

    public static void main(String[] args) {
        ArrayTest class1 = new ArrayTest();

        start = System.currentTimeMillis();
        class1.createArray();
        finish = System.currentTimeMillis();
        temp = finish - start;
        System.out.println("Array:");
        System.out.println("start " + start + " finish " + finish);
        System.out.println("time: " + temp+" milliseconds");

        start = System.currentTimeMillis();
        class1.createArrayList();
        finish = System.currentTimeMillis();
        temp = finish - start;
        System.out.println("ArrayList:");
        System.out.println("start " + start + " finish " + finish);
        System.out.println("time: " + temp+" milliseconds");

        start = System.currentTimeMillis();
        class1.createLinkedList();
        finish = System.currentTimeMillis();
        temp = finish - start;
        System.out.println("LinkedList:");
        System.out.println("start " + start + " finish " + finish);
        System.out.println("time: " + temp+" milliseconds");

    }

    public void createArray() {
        for (int i = 0; i < size; i++) {
            iArr[i] = item;
        }
    }

    public void createArrayList() {
        for (int i = 0; i < size; i++) {
            iArrayList.add(item);
        }
    }

    public void createLinkedList() {
        for (int i = 0; i < size; i++) {
            iLinkedList.add(item);
        }
    }
}

10 000 iterations - Integer

1.1 


1.2


1 000 000 iterations - Integer

1.1


1.2


10 000 iterations - int

1.1


1.2


1 000 000 iterations - int

1.1


1.2


Summary:
Array at least 6 times faster than Collection!

No comments:

Post a Comment