- 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 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.
- 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.
if the content is not visible, click on the image and zoom it to read.
Difference between ArrayList and 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