Collection Framework - Introduction

Collection typically stands for a group of things or people. In programming terms, you can think of it as a collection of data objects.

Why Collections?
Prior to introduction of collections framework, Array was used to group objects.

Lets first see the limitations of Arrays and then see the offerings of the collections framework.

Limitations of Arrays:
- Fixed size : can lead to underutilization/overutilization based on demand
- Can store same type of objects : int[] can have only int types; String[] can only store String objects.

To overcome the above limitations, we make use of collections.

1. Collections are dynamically sized.
2. Collections can hold both same type as well as different type of objects. i.e. they can store both homogenous as well as heterogenous objects.

Apart from the above points, collections are implemented based on data structures, so there are readymade implementations available like contains(), natural ordering of elements, etc. In case of arrays, even simple logics need to be implemented by hand.

Performance wise, arrays are always recommended but memory wise collections are recommended.
Collections allocate memory as and when required, while arrays are fixed size, so memory gets allocated initially only. While when it comes to performance, arrays outperform collections, since collections are growable so it takes a troll on performance whenever a new elements needs to be added.

One major difference between arrays and collections : With arrays, we can hold primitive data types as well as Objects; while Collections can only hold Objects.








Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java

Hashed data structures