ArrayList is a mostly used collection class in java. This is used to store different lists of objects

We have seen List in Java. Now we will see how we can store elements in ArrayList in java and how to use different methods for array lists.

ArrayList

  1. java.util.ArrayList is a mostly used collection class in java. This is used to store different lists of objects. 
  2. Since in java array we need to define size at the time of declaration itself as shown.

String[] strArray = new String[5];

In the above, we can not increase the size of an array later since the maximum size of an array is 5.  

  1. To overcome this issue we can use ArrayList to store the data. Array list starts with default size – 10. Array List grows automatically when more data is added to it. 
  2. We can also provide initial capacity to an array list while creating an object.

ArrayList has Three ways to create an object.

  1. ArrayList<String> list = new ArrayList<String>();

This will create an empty list with initial default capacity.

  1. ArrayList<String> list = new ArrayList<String>(20);

This will create an empty list with a capacity of 20. Here 20 is the initial capacity. When we know that array is required to store huge data so we can initially create the array with that capacity this will save the time required to grow and array and reallocate the elements. We can not pass negative numbers as initial capacity else IllegalArgumentException will be thrown.

  1. ArrayList<String> list = new ArrayList<String>(oldList);

This will create an object by taking another list as a parameter and add into the new array list in the sequence it’s returned by an iterator. This will throw NullPointerException if oldList is null.

Let’s see the example below for the using above 3 constructors.

Let’s see different ArrayList methods that are commonly used.

  1. add(E e), 
  2. add(int index, E e),
  3. addAll(Collection <? extends E> c),
  4. addAll(int index, Collection <? extends E> c)

These methods are used for adding elements or another list into another list. With index position given that element/list will be added at that index position else, it will be added at the end (appended) of the list. An example for add is as below.

  1. contains(E e) :  To check if element is present inside the list. 
  2. isEmpty(): To check if list is empty.
  3. indexOf(E e): To check the index position of an element (first occurrence).
  4. lastIndexOf(E e): This will return index position of last occurrence of element.
  1. forEach(Consumer<? super E> action) : iterate over each element in the list.
  1. iterator(): returns the iterator for the elements in the list, it returns a fail-fast iterator (i.e operations are performed on the original list). For the above list instead of forEach we can also print elements using java.util.Iterator 
  1. listIterator(): This returns the list iterator which is also a fail-fast iterator. This is the same as the Iterator shown above. We are using java.util.ListIterator
  1. listIterator(int index): This returns the list iterator starting from the specified index. In the below example elements will be printed starting from index 1.
  1.  clear(): removes all the elements from the list as shown in the below example.
  1. remove(int index): removes the element from specified index position.
  1. remove(Element e): removes an element from the list if it’s present. If that element is not present then the list will remain as it is.
  1. removeAll(Collection c): Removes all the elements given in the collection from the list if present.
  1. removeIf(Predicate<? super E> filter): Removes all the elements from the list which satisfy the given condition.
  1. retainAll(Collection<E> c):  Retains only those elements which are present in the provided collection and deletes the remaining.
  1.  replaceAll(UnaryOperator<E> operator): Replaces each element of this list with the result of unary operations.
  1. get(int index): To get the element from a particular index position.
  2. set(int index, E e): to replace the element at a particular index position.
  3. subList(int fromIndex, int toIndex): To get the sublist from an original list with from index(inclusive) and to index(exclusive) specified.
  1. spliterator(): To create late-binding and fail-fast Spliterator. Using forEachRemaining method of Spliterator we can iterate over the element o the list.
  1. ensureCapacity(int minCapacity): To increase the capacity of  ArrayList instance with the minCapacity provided.
  2. size(): This will return the size of the list (no. of elements present in the list).
  3. trimToSize(): This will trim the capacity of the list to its actual size. 
  1. sort(Comparator<? super E> c): This will sort the 
  1. toArray(): This will return the new array object with all the elements from first to last in the sequence.
  2. toArray(T[] a): This will return the new array object where the runtime type of the returned array is that of the specified array.

-A blog by Shwetali Khambe

Related Posts