java util ArrayList Class - ArrayList Class in Java
Java ArrayList Tutorial with Examples
ArrayList in Java is used to store dynamically sized collection of elements. Java Arrays are fixed in size, an ArrayList grows its size automatically when new elements are added to it. ArrayList is part of Java's collection framework and implements Java's List interface. Java ArrayList is not synchronized. ArrayList is a part of collection framework and is present in java.util package.
The ArrayList class is the Collection Framework's replacement for the Vector class. Functionally equivalent, their primary difference is that ArrayList usage is not synchronized by default, whereas Vector is. Both maintain their collection of data in an ordered fashion within an array as their backing store.
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
What are the Features of ArrayList in Java ?
- ArrayList inherits AbstractList class and implements List interface.
- An ArrayList is a re-sizable array, also called a dynamic array. It grows its size to accommodate new elements and shrinks the size when the elements are removed.
- ArrayList internally uses an array to store the elements. Just like arrays, It allows you to retrieve the elements by their index.
- Java ArrayList is an ordered collection. It maintains the insertion order of the elements.
- Java ArrayList allows duplicate and null values.
- Java ArrayList is not synchronized.
- You cannot create an ArrayList of primitive types like int, char etc. You need to use boxed types like Integer, Character, Boolean etc.
Is ArrayList Thread Safe ?
The ArrayList implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.
To prevent accidental unsynchronized access to the list, you should create list with Collections.synchronizedList method.
What is the ArrayList Performance ?
The array provides quick, random access to elements at a cost of slower insertion and deletion of those elements not at the end of the list. If you need to frequently add and delete elements from the middle of the list, consider using a LinkedList.
What are the Similarities found in ArrayList ?
ArrayList shares some similarities with HashSet and TreeSet and provides some behavior that is not the same. The base implementation class is similar to HashSet and TreeSet both extend from the AbstractCollection superclass. However, instead of further extending from AbstractSet, ArrayList extends from AbstractList. Unlike the sets, ArrayList supports storing duplicate elements. While much of the ArrayList behavior is inherited from AbstractList, the class still needs to customize the majority of its behavior.
In this tutorial you can learn about java.util.ArrayList class and its examples. And also learn how to use java.util.ArrayList class.
How do you declare an ArrayList in Java ?
You can declare Java ArrayList as follows.
ArrayList<String> list = new ArrayList<String>(); //Creating arraylist.
The following Java ArrayList Example shows
- How to declare an ArrayList
- How to create an ArrayList using the ArrayList() constructor.
- How to Add new elements to an ArrayList using the add() method.
- How to check ArrayList contains how many elements.
- How to use ArrayList in Java.
How to Create an ArrayList and How to add new elements to it ?
This example shows:
- How to create an ArrayList using the ArrayList() constructor.
- Add new elements to an ArrayList using the add() method.
Creating an ArrayList and adding new elements to it Example
How to use ArrayList with Iterator ?
The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast.
This method do not accept any parameter. The ArrayList.iterator() method returns an Iterator over the elements in this list in proper sequence. If you want to use Iterator follow the following steps
- Obtain an iterator to the start of the collection by calling the collection's iterator() method.
- Set up a loop that makes a call to Iterator.hasNext(). Have the loop iterate as long as Iterator.hasNext() returns true.
- Within the loop, obtain each element by calling next().
The following example shows how to use ArrayList with Iterator.
How to use ArrayList with Iterator
How to Create an ArrayList from another Collection ?
An ArrayList can be created from another collection using the java.util.Arrays.asList() method. The following program demonstrates :
- How to create an ArrayList from another collection using the ArrayList(Collection c) constructor.
- How to add all the elements from an existing collection to the new ArrayList using the addAll() method.
How to Create an ArrayList from another Collection Example
How to check if an ArrayList is empty ?
You can check if an ArrayList is empty using the ArrayList.isEmpty() method.
How to find the size of an ArrayList ?
You can find the size of an ArrayList using the ArrayList.size() method.
How to access the element at a particular index in an ArrayList ?
You can access the element at a particular index in an ArrayList using the ArrayList.get() method.
How to modify the element at a particular index in an ArrayList ?
You can modify the element at a particular index in an ArrayList using the ArrayList.set() method.
The following example shows how to use ArrayList with Arrays, List and Iterator.
java.util.ArrayList class Example 3
java.util.ArrayList class Example 4
Removing elements from an ArrayList
How to remove the element at a given index in an ArrayList ?
You can remove the element at a given index in an ArrayList using ArrayList.remove(int index) method.
How to remove an element from an ArrayList ?
You can remove matched element from an ArrayList using ArrayList.remove(Object o) method.
How to remove all the elements from an ArrayList ?
You can remove all the elements from an ArrayList using ArrayList.removeAll() method.
How to remove all the elements matching a given predicate from an ArrayList ?
You can remove all the elements matching a given predicate from an ArrayList using ArrayList.removeIf() method.
How to clear an ArrayList ?
You can clear an ArrayList using ArrayList.clear() method.
The following example shows how to compare two List Collections.
How to compare two List Collections
The following example shows how to save List Collection into file.
How to save List Collection into file
The following example shows how to retrieve List Collection from file.
How to retrieve List Collection from file
The following example shows how to use ArrayList with Arrays, List and ListIterator.
How to use ArrayList with Arrays