ArrayList Methods with Examples in Java – Programming Code Examples

ArrayList Methods with Examples in Java17 min read

ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package.

Java ArrayList add()

Example 1: The Java ArrayList add() method inserts an element to the arraylist at the specified position.


Java ArrayList addAll()

The Java ArrayList addAll() method adds all the element of a collection to the arraylist.

Example 2: Inserting Elements using ArrayList addAll()


Example 3: Inserting Elements to the Specified Position


Example 4: Inserting Elements from HashSet to ArrayList


Java ArrayList clear()

The Java ArrayList clear() method removes all the elements from an arraylist.

Example 5: Remove all elements from String Type ArrayList


Java ArrayList clone()

The Java ArrayList clone() method makes the shallow copy of an array list.

Example 7:


Java ArrayList contains()

The Java ArrayList contains() method checks if the specified element is present in the arraylist.

Example 8: contains() Method with String ArrayList


Java ArrayList get()

The Java ArrayList get() method returns the element present in specified position.

Example 9: get() Method with String ArrayList


Java ArrayList indexOf()

The Java ArrayList indexOf() method returns the position of the specified element in the arraylist.

Example 10: Get the Index of ArrayList Element


Example 11: Get the Position of the First Occurrence of an Element


Java ArrayList removeAll()

The Java ArrayList removeAll() method removes all the elements from the arraylist that are also present in the specified collection.

Example 12: Remove all elements from an ArrayList


Example 13: Remove all Elements from an ArrayList Present in Another ArrayList


Example 14: Remove all Elements from an ArrayList Present in a HashSet


Java ArrayList remove()

The Java ArrayList remove() method removes the single element from the arraylist.

Example 15: Remove the Specified Element from the ArrayList.


Example 16: Remove the Element From the Specified Position.


Java ArrayList size()

The Java ArrayList size() method returns the number of elements present in the arraylist.

Example 17: Get the Length of an ArrayList


Java ArrayList isEmpty()

The Java ArrayList isEmpty() method checks if the arraylist is empty.

Example 18: Check if ArrayList is Empty.


Java ArrayList subList()

The Java ArrayList subList() method extracts a portion of the arraylist and returns it.

Example 19: Get a Sub List From an ArrayList.


Java ArrayList set()

The Java ArrayList set() method replaces the element present in a specified position with the specified element in an arraylist.

Example 20: Replace an Element in ArrayList.


Java ArrayList sort()

The Java ArrayList sort() method sorts the elements in an arraylist according to the specified order.

Example 21: Sort the ArrayList in Natural Order


Example 22: Sort the ArrayList in Reverse Order


Java ArrayList toArray()

The Java ArrayList toArray() method converts an arraylist into an array and returns it.

Example 23: ArrayList toArray() Method with Parameter.


Example 24: ArrayList toArray() Method without Parameter.


Java ArrayList toString()

The Java ArrayList toString() method converts an arraylist into a String.

Example 25: Convert ArrayList to String


Java ArrayList ensureCapacity()

The Java ArrayList ensureCapacity() method sets the size of an arraylist with the specified capacity.

Example 26: Java ArrayList ensureCapacity()


Java ArrayList lastIndexOf()

The Java ArrayList lastIndexOf() method returns the position of the last occurrence of the specified element.

Example 27: Get the Last Occurrence of ArrayList Element


Java ArrayList retainAll()

The Java ArrayList retainAll() method retains only those elements in the arraylist that are also present in the specified collection.

Example 28: Java ArrayList retainAll()


Java ArrayList containsAll()

The Java ArrayList containsAll() method checks whether the arraylist contains all the elements of the specified collection.

Example 29: Java ArrayList containsAll()

Java ArrayList trimToSize()

The Java ArrayList trimToSize() method trims (sets) the capacity of the arraylist equal to the number of elements in the arraylist.

Example 30: Java ArrayList trimToSize()


Java ArrayList removeRange()

The Java ArrayList removeRange() method removes elements from the arraylist present in between the specified indices.

Example 31: Java ArrayList removeRange()


Java ArrayList replaceAll()

The Java ArrayList replaceAll() method replaces each elements of the arraylist with the result specified by the parameter.

Example 32: Change All Elements to Uppercase


Example 33: First Letter of ArrayList Elements


Java ArrayList removeIf()

The Java ArrayList removeIf() method removes all elements from the arraylist that satisfy the specified condition.

Example 34: Remove languages With “C” in Name


Java ArrayList forEach()

The Java ArrayList forEach() method performs the specified action on each element of the arraylist one by one.

Example 35: Java ArrayList forEach()


Java ArrayList iterator()

The Java ArrayList iterator() method returns an iterator to access each element of the arraylist in a proper sequence.

Example 36: Java ArrayList iterator()


Leave a Comment