Skip to content

Collections in java

What are Collections in java?

A collection is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.

java Collections framework

Advantage of Collections:

  • Collections are growable in nature i.e based on our requirement we can increase and decrease the size.
  • Collections can hold both homogeneous and heterogeneous elements.
  • Every collection class is implemented based on some standard data structure. Hence readymade method support is available for every requirement. Being a programmer we have to use method and we are not responsible to provide implementation.

The disadvantage of Collections :

  • Performance point of view collections is not recommended to use. This is the limitation of collection.

Which concept is recommended either arrays or collection?

If you know the size in advance highly recommended concept is Array.

Reason: In collection we are growbable in nature(We are not getting free of cost we have to pay something is performance.)

Example: Take one array of size 10.

          

0                1                 2               3               4                5                 6                7                8              9

A

If 11th element is coming array will going to tell I can’t provide support.

But assume it is Arraylist then 10th element we inserted

          

0                1                2                3                4                5                 6               7                8               9

A

Now if 11th element comes. An Arraylist is flexible it fit based on our requirement we can increase or decrease the size.

Hows it work?

How it’s 11th element going to be inserted don’t feel another memory will be created and it will insert or 11th element going to be store.

Once Arraylist reaches it’s max capacity a bigger Arraylist memory is going to be created.

          

0                1               2                3                4               5                 6               7               8               9

AL                                                             COPYING

           

0                1                2                3               4               5               6                7                8                9               10

AL(Reassign)

11th element will be inserted

After copying all the data into another Arraylist garbage collector will de-allocate the first arraylist memory.

          

0                1                2                3                4                5                 6               7                8               9

A

GARBAGE COLLECTION

  • If we have to store 11th element here it’s not a big problem, But if we have 1 crore element and then we have to insert one more element it will going to take a month or 1 year.
  • So, Performance wise collections are not up to the mark.

Leave a Reply

Your email address will not be published. Required fields are marked *