In the previous post, we have learnt about collection interface and list interface. In this post, we will talk about ArrayList. ArrayList is a child class of list interface.
Topics covered:
It is used to build an ArrayList that has the specified initial capacity.
It is used to build an ArrayList that is initialized with the elements of the collection c.
Usually, we can use collections to hold and transfer objects from one place to another and to provide this requirement, every collection already supports serializable and cloneable interfaces.
ArrayList and vector implement RandomAccess Interface so we can access any element at the same speed.
if the content is not visible, click on the image and zoom it to read.
Since methods of ArrayList is non-synchronized so threads are not required to wait to operate on ArrayList hence relatively performance is high.
At a time, only one thread is allowed to operate on vector object so threads are required to wait to operate hence relatively performance is low.
By default ArrayList object is non-synchronized but we can get the synchronized version of ArrayList by using collection class SynchronizedList() method.
For more Read here Documentation
Topics covered:
- Introduction of ArrayList in java.
- ArrayList Constructors.
- ArrayList Methods.
- RandomAccess Interface.
- Difference between ArrayList and Vector.
- Cheat code for ArrayList
Introduction of ArrayList
- The underlying data structure is resizable Array or growable array.
- Duplicates are allowed.
- Insertion order is preserved.
- Heterogeneous objects are allowed. – Except for TreeSet and TreeMap everywhere, heterogeneous objects are allowed.
- Null insertion is possible.
ArrayList Constructors:
- ArrayList l = new ArrayList();
Creates an empty ArrayList objects with default initial capacity 10. Once ArrayList reaches its max capacity, a new ArrayList will be created with new capacity.
New capacity = (current capacity * 3/2) + 1
- ArrayList l = new ArrayList(int initial_capacity);
It is used to build an ArrayList that has the specified initial capacity.
- ArrayList l = new ArrayList(Collection c);
It is used to build an ArrayList that is initialized with the elements of the collection c.
Every collection implements the serializable and cloneable interface.
Usually, we can use collections to hold and transfer objects from one place to another and to provide this requirement, every collection already supports serializable and cloneable interfaces.
ArrayList and vector implement RandomAccess Interface so we can access any element at the same speed.
So, if our main operation is retrieval then highly go for ArrayList.
RandomAccess Interface:
- It is present in java.util package.
- Supported by ArrayList and vector.
- Has no methods and it is a marker interface.
If our main operation is insertion/deletion from the middle then ArrayList is not recommended.
ArrayList Methods
if the content is not visible, click on the image and zoom it to read.
ArrayList Example
Difference between ArrayList and Vector
ArrayList | Vector |
Methods are non-synchronized in nature. | Methods are synchronized in nature. |
Objects are thread safe. | Objects are not thread safe. |
Performance is relatively high. | Performance is comparatively low. |
Introduced in JDK 1.2 | Introduced in JDK 1.0 |
It is non-legacy class. | It is a legacy class. |
Since methods of ArrayList is non-synchronized so threads are not required to wait to operate on ArrayList hence relatively performance is high.
At a time, only one thread is allowed to operate on vector object so threads are required to wait to operate hence relatively performance is low.
How to get the Synchronized version of an ArrayList object?
ArrayList l = new ArrayList();
By default ArrayList object is non-synchronized but we can get the synchronized version of ArrayList by using collection class SynchronizedList() method.
Public static list SynchonizedList(List l)
List l1 = Collections.SynchronizedList(l);
ArrayList cheat codes
For more Read here Documentation
Comments
Post a Comment