Complete Guide to ArrayList class.

ArrayList class is one of the most used and most basic class of Collection framework. It provides interesting capabilities and is build on array.

Below are series of articles on ArrayList class. There are tons of examples to understand the concepts.

  1. ArrayList constructors : How to create ArrayList instances?
    1. public ArrayList()
    2. public ArrayList(int initialCapacity)
    3. public ArrayList(Collection<? extends E> c)
  2. How to add/get/set elements in ArrayList?
    1. boolean add(E e)
    2. void add(int index, E element)
    3. boolean addAll(Collection<? extends E> c)
    4. boolean addAll(int index, Collection<? extends E> c)
    5. E get(int index)
    6. E set(int index, E element)
    7. List<E> subList(int fromIndex, int toIndex)
    8. boolean retainAll(Collection<?> c)
  3. How to remove elements from ArrayList?
    1. boolean remove(Object o)
    2. E remove(int index)
    3. boolean removeAll(Collection<?> c)
  4. Check if elements exists in ArrayList?
    1. int indexOf(Object o)
    2. int lastIndexOf(Object o)
    3. boolean contains(Object o)
    4. boolean containsAll(Collection<?> c)
  5. MISC Methods
    1. size()
    2. isEmpty()
    3. clear()
    4. hashCode()
    5. equals()
    6. trimToSize()
  6. toArray methods
    1. toArray()
    2. toArray(T[] a)
  7. Java 9 onwards methods
    1. default void replaceAll(UnaryOperator<E> operator)
    2. default void sort(Comparator<? super E> c)
    3. default Spliterator<E> spliterator()
    4. static <E> List<E> of()
    5. static <E> List<E> of(E e1)
    6. static <E> List<E> of(E e1, E e2)
    7. static <E> List<E> of(E e1, E e2, E e3)
    8. static <E> List<E> of(E e1, E e2, E e3, E e4)
    9. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5)
    10. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
    11. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
    12. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
    13. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
    14. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
    15. static <E> List<E> of(E… elements)
    16. static <E> List<E> copyOf(Collection<? extends E> coll)
  8. Other methods that are common to Collection interface
    1. default boolean removeIf(Predicate<? super E> filter)
    2. default <T> T[] toArray(IntFunction<T[]> generator)
    3. default Spliterator<E> spliterator()
    4. default Stream<E> stream()
    5. default Stream<E> parallelStream()
  9. Serialize and DeSerialize ArrayList