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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println(languages); } } |
Output:
1 2 3 | [C#, Java, C++, PHP, Python] |
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); ArrayList<String> script = new ArrayList<String>(); script.add("PHP"); script.add("Python"); languages.addAll(script); System.out.println(languages); } } |
Output:
1 2 3 | [C#, Java, C++, PHP, Python] |
Example 3: Inserting Elements to the Specified Position
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); ArrayList<String> script = new ArrayList<String>(); script.add("PHP"); script.add("Python"); languages.addAll(2,script); System.out.println(languages); } } |
Output:
1 2 3 | [C#, Java, PHP, Python, C++] |
Example 4: Inserting Elements from HashSet to ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.ArrayList; import java.util.HashSet; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); // create a hashset of String type HashSet<String> script = new HashSet<>(); // add elements to the hashset script.add("Python"); script.add("JavaScript"); languages.addAll(script); System.out.println(languages); } } |
Output:
1 2 3 | [C#, Java, C++, JavaScript, Python] |
Java ArrayList clear()
The Java ArrayList clear() method removes all the elements from an arraylist.
Example 5: Remove all elements from String Type ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); System.out.println("Programming Languages: " + languages); // remove all elements languages.clear(); System.out.println("ArrayList after clear(): " + languages); } } |
Output:
1 2 3 4 | Programming Languages: [C#, Java, C++] ArrayList after clear(): [] |
Java ArrayList clone()
The Java ArrayList clone() method makes the shallow copy of an array list.
Example 7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); System.out.println("ArrayList: " + languages); ArrayList<String> cloneLang = (ArrayList<String>)languages.clone(); System.out.println("Cloned ArrayList: " + cloneLang); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++] Cloned ArrayList: [C#, Java, C++] |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); System.out.println("ArrayList: " + languages); System.out.print("Is Java present in the arraylist: "); System.out.println(languages.contains("Java")); System.out.print("Is PHP present in the arraylist: "); System.out.println(languages.contains("PHP")); } } |
Output:
1 2 3 4 5 | ArrayList: [C#, Java, C++] Is Java present in the arraylist: true Is PHP present in the arraylist: false |
Java ArrayList get()
The Java ArrayList get() method returns the element present in specified position.
Example 9: get() Method with String ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); System.out.println("ArrayList: " + languages); // access element at index 1 String element = languages.get(1); System.out.println("Element at index 1: " + element); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++] Element at index 1: Java |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); System.out.println("ArrayList: " + languages); // find the position of C++ int position1 = languages.indexOf("C++"); System.out.println("Index of C++: " + position1); // find the position of PHP int position2 = languages.indexOf("PHP"); System.out.println("Index of PHP: " + position2); } } |
Output:
1 2 3 4 5 | ArrayList: [C#, Java, C++] Index of C++: 2 Index of PHP: -1 |
Example 11: Get the Position of the First Occurrence of an Element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("C"); languages.add("Java"); System.out.println("ArrayList: " + languages); // find the position of JAVA int pos = languages.indexOf("Java"); System.out.println("First index of Java: " + pos); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, C, Java] First index of Java: 1 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); // remove all elements from arraylist languages.removeAll(languages); System.out.println("ArrayList after removeAll(): " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, Go, Python] ArrayList after removeAll(): [] |
Example 13: Remove all Elements from an ArrayList Present in Another ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); ArrayList<String> languages2 = new ArrayList<>(); languages2.add("Go"); languages2.add("Python"); languages.removeAll(languages2); System.out.println("ArrayList after removeAll(): " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, Go, Python] ArrayList after removeAll(): [C#, Java, C++] |
Example 14: Remove all Elements from an ArrayList Present in a HashSet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.ArrayList; import java.util.HashSet; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); // create a HashSet HashSet<String> languages2 = new HashSet<>(); // add elements to the HashSet languages2.add("Go"); languages2.add("Python"); System.out.println("HashSet: " + languages2); languages.removeAll(languages2); System.out.println("ArrayList after removeAll(): " + languages); } } |
Output:
1 2 3 4 5 | ArrayList: [C#, Java, C++, Go, Python] HashSet: [Go, Python] ArrayList after removeAll(): [C#, Java, C++] |
Java ArrayList remove()
The Java ArrayList remove() method removes the single element from the arraylist.
Example 15: Remove the Specified Element from the ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); // remove the element Java boolean result = languages.remove("Java"); System.out.println("Is element Java removed? " + result); System.out.println("ArrayList after remove(): " + languages); } } |
Output:
1 2 3 4 5 | ArrayList: [C#, Java, C++, Go, Python] Is element Java removed? true ArrayList after remove(): [C#, C++, Go, Pyth |
Example 16: Remove the Element From the Specified Position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); // remove the element from position 2 String element = languages.remove(2); System.out.println("ArrayList after remove(): " + languages); System.out.println("Removed Element: " + element); } } |
Output:
1 2 3 4 5 | ArrayList: [C#, Java, C++, Go, Python] ArrayList after remove(): [C#, Java, Go, Python] Removed Element: C++ |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); // get the number of elements of arraylist int size = languages.size(); System.out.println("Length of ArrayList: " + size); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, Go, Python] Length of ArrayList: 5 |
Java ArrayList isEmpty()
The Java ArrayList isEmpty() method checks if the arraylist is empty.
Example 18: Check if ArrayList is Empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); // checks if the ArrayList has any element boolean result = languages.isEmpty(); // false System.out.println("Is the ArrayList empty? " + result); languages.clear(); System.out.println("Updated ArrayList: " + languages); // checks if the ArrayList is empty result = languages.isEmpty(); // true System.out.println("Is the ArrayList empty? " + result); } } |
Output:
1 2 3 4 5 6 | ArrayList: [C#, Java, C++, Go, Python] Is the ArrayList empty? false Updated ArrayList: [] Is the ArrayList empty? true |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("Go"); languages.add("Python"); System.out.println("ArrayList: " + languages); System.out.println("SubList: " + languages.subList(3, 5)); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, Go, Python] SubList: [Go, Python] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // replace element at index 3 String element = languages.set(3, "PHP8"); System.out.println("Updated ArrayList: " + languages); System.out.println("Replaced Element: " + element); } } |
Output:
1 2 3 4 5 | ArrayList: [C#, Java, C++, PHP, Python] Updated ArrayList: [C#, Java, C++, PHP8, Python] Replaced Element: PHP |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; import java.util.Comparator; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // sort the ArrayList in ascending order languages.sort(Comparator.naturalOrder()); System.out.println("Sorted ArrayList: " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Sorted ArrayList: [C#, C++, Java, PHP, Python] |
Example 22: Sort the ArrayList in Reverse Order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; import java.util.Comparator; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // sort the ArrayList in ascending order languages.sort(Comparator.reverseOrder()); System.out.println("Sorted ArrayList: " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Sorted ArrayList: [Python, PHP, Java, C++, C#] |
Java ArrayList toArray()
The Java ArrayList toArray() method converts an arraylist into an array and returns it.
Example 23: ArrayList toArray() Method with Parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // Create a new array of String type // size of array is same as the ArrayList String[] arr = new String[languages.size()]; // Convert ArrayList into an array languages.toArray(arr); // print all elements of the array System.out.print("Array: "); for(String item:arr) { System.out.print(item+", "); } } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Array: C#, Java, C++, PHP, Python, |
Example 24: ArrayList toArray() Method without Parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // Convert ArrayList into an array // the method has no parameter Object[] obj = languages.toArray(); // print all elements of the array System.out.print("Array: "); for(Object item : obj) { System.out.print(item+", "); } } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Array: C#, Java, C++, PHP, Python, |
Java ArrayList toString()
The Java ArrayList toString() method converts an arraylist into a String.
Example 25: Convert ArrayList to String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // convert ArrayList to String String list = languages.toString(); System.out.println("String: " + list); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] String: [C#, Java, C++, PHP, Python] |
Java ArrayList ensureCapacity()
The Java ArrayList ensureCapacity() method sets the size of an arraylist with the specified capacity.
Example 26: Java ArrayList ensureCapacity()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages= new ArrayList<>(); // set the capacity of the arraylist languages.ensureCapacity(3); // Add elements in the ArrayList languages.add("Java"); languages.add("Python"); languages.add("C"); System.out.println("ArrayList: " + languages); } } |
Output:
1 2 3 | ArrayList: [Java, Python, C] |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("Programming Languages: " + languages); // get the position of Java occurred last int position1 = languages.lastIndexOf("Java"); System.out.println("Last Occurrence of Java: " + position1); // C is not in the ArrayList // Returns -1 int position2 = languages.lastIndexOf("C"); System.out.println("Last Occurrence of C: " + position2); } } |
Output:
1 2 3 4 5 | Programming Languages: [C#, Java, C++, PHP, Python] Last Occurrence of Java: 1 Last Occurrence of C: -1 |
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("Programming Languages: " + languages); // create another ArrayList ArrayList<String> languages2 = new ArrayList<>(); // add elements to ArrayList languages2.add("Go"); languages2.add("Java"); languages2.add("Python"); System.out.println("ArrayList 2: " + languages2); // retain the common element languages.retainAll(languages2); System.out.println("Common Elements: " + languages); } } |
Output:
1 2 3 4 5 | Programming Languages: [C#, Java, C++, PHP, Python] ArrayList 2: [Go, Java, Python] Common Elements: [Java, Python] |
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList 1: " + languages); // create another ArrayList ArrayList<String> languages2 = new ArrayList<>(); // add elements to ArrayList languages2.add("Java"); languages2.add("Python"); System.out.println("ArrayList 2: " + languages2); // check if ArrayList 1 contains ArrayList 2 boolean result1 = languages.containsAll(languages2); System.out.println("ArrayList 1 contains all elements of ArrayList 2: " + result1); // check if ArrayList 2 contains ArrayList 1 boolean result2 = languages2.containsAll(languages); System.out.println("ArrayList 2 contains all elements of ArrayList 1: " + result2); } } |
1 2 3 4 5 6 | ArrayList 1: [C#, Java, C++, PHP, Python] ArrayList 2: [Java, Python] ArrayList 1 contains all elements of ArrayList 2: true ArrayList 2 contains all elements of ArrayList 1: false |
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // trim capacity to 3 languages.trimToSize(); System.out.println("Size of ArrayList: " + languages.size()); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Size of ArrayList: 5 |
Java ArrayList removeRange()
The Java ArrayList removeRange() method removes elements from the arraylist present in between the specified indices.
Example 31: Java ArrayList removeRange()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main extends ArrayList{ public static void main(String[] args) { Main languages = new Main(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // remove elements between 1 to 3 languages.removeRange(3,5); System.out.println("Updated ArrayList: " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Updated ArrayList: [C#, Java, C++] |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // replace all elements to uppercase languages.replaceAll(e -> e.toUpperCase()); System.out.println("Updated ArrayList: " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Updated ArrayList: [C#, JAVA, C++, PHP, PYTHON] |
Example 33: First Letter of ArrayList Elements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); languages.replaceAll(e -> e.substring(0,1)); System.out.println("Updated ArrayList: " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] Updated ArrayList: [C, J, C, P, P] |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); languages.removeIf(e -> e.contains("C"));; System.out.println("languages without C: " + languages); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] languages without C: [Java, PHP, Python] |
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); languages.forEach((e) -> { System.out.print(e + " "); }); } } |
Output:
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] C# Java C++ PHP Python |
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { ArrayList<String> languages = new ArrayList<String>(); languages.add("C#"); languages.add("Java"); languages.add("C++"); languages.add("PHP"); languages.add("Python"); System.out.println("ArrayList: " + languages); // Create a variable of Iterator // store the iterator returned by iterator() Iterator<String> iterate = languages.iterator(); System.out.print("ArrayList: "); // loop through ArrayList till it has all elements // Use methods of Iterator to access elements while(iterate.hasNext()){ System.out.print(iterate.next()); System.out.print(", "); } } } |
1 2 3 4 | ArrayList: [C#, Java, C++, PHP, Python] ArrayList: C#, Java, C++, PHP, Python, |