Collection FrameWork!!

What is Collection in Java?

laiba Memon
Nov 4 · 3 min read

A Collection represents a single unit of objects, i.e., a group.

What is a framework in Java?

  • It provides readymade architecture.
  • It represents a set of classes and interfaces.
  • It is optional.

What is Collection framework?

The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has:

  1. Interfaces and its implementations, i.e., classes

So,Now i Start to Discuss about the ArrayList…

What Actually the ArrayList is?

The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate element of different data types. The ArrayList class maintains the insertion order and is non-synchronized. The elements stored in the ArrayList class can be randomly accessed.

Why ArrayList is better than Array?

The limitation with array is that it has a fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink.

On the other hand ArrayList can dynamically grow and shrink after addition and removal of elements .Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy.

How to create an ArrayList?

We can create an ArrayList by writing a simple statement like this:

This statement creates an ArrayList with the name arr with type “String”. The type determines which type of elements the list will have. Since this list is of “String” type, the elements that are going to be added to this list will be of type “String”.

ArrayList<String> arr=new ArrayList<String>();

Similarly we can create ArrayList that accepts int elements.

ArrayList<Integer> arr=new ArrayList<Integer>();

How to add elements to an ArrayList?

We add elements to an ArrayList by using add() method, this method has couple of variations, which we can use based on the requirement. For example: If we want to add the element at the end of the List then simply do it like this:

arr.add("Smith"); //This will add "Smith" at the end of List

To add the element at the specified location in ArrayList, we can specify the index in the add method like this:

arr.add(3, "Smith"); //This will add "Steve" at the fourth position

lets Write the Code as an Example 1:

Output is Something Like that:

Example 2:

Output:

There are some Methods in ArrayList u can open these methods through this Link.

https://www.geeksforgeeks.org/arraylist-in-java/

Thats All ……….ThankYou!!!!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade