ArrayList Methods with Examples in Java – Programming Code Examples
Java

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.




Output:

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()

Output:

Example 3: Inserting Elements to the Specified Position

Output:

Example 4: Inserting Elements from HashSet to ArrayList

Output:

Java ArrayList clear()

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

Example 5: Remove all elements from String Type ArrayList

Output:

Java ArrayList clone()

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

Example 7:

Output:

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

Output:

Java ArrayList get()

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

Example 9: get() Method with String ArrayList

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

Java ArrayList remove()

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

Example 15: Remove the Specified Element from the ArrayList.

Output:

Example 16: Remove the Element From the Specified Position.

Output:

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

Output:

Java ArrayList isEmpty()

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

Example 18: Check if ArrayList is Empty.

Output:

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.

Output:

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.

Output:

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

Output:

Example 22: Sort the ArrayList in Reverse Order

Output:

Java ArrayList toArray()

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

Example 23: ArrayList toArray() Method with Parameter.

Output:

Example 24: ArrayList toArray() Method without Parameter.

Output:

Java ArrayList toString()

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

Example 25: Convert ArrayList to String

Output:

Java ArrayList ensureCapacity()

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

Example 26: Java ArrayList ensureCapacity()

Output:

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

Output:

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()

Output:

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()

Output:

Java ArrayList removeRange()

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

Example 31: Java ArrayList removeRange()

Output:

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

Output:

Example 33: First Letter of ArrayList Elements

Output:

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

Output:

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()

Output:

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()

Output:

Leave a Comment