How to remove elements from ArrayList
In this post we will see various ways to remove elements from ArrayList in java.
ArrayList provides following methods to remove elements.
1. E remove(int index)
Removes an element from the specified position in the arrayList
Example
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 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListRemoveExample1 { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); //Print the ArrayList System.out.println("Printing the Elements of ArrayList"); for(String element : arrayList1){ System.out.println(element); } // Remove the 2nd element arrayList1.remove(1); System.out.println("***Removed the 2nd Element. Now Printing the Elements of ArrayList"); for(String element : arrayList1){ System.out.println(element); } } } |
Output
1 2 3 4 5 6 7 | Printing the Elements of ArrayList Java JSP JavaScript ***Removed the 2nd Element. Now Printing the Elements of ArrayList Java JavaScript |
2. boolean remove(Object o)
Removes the first occurrence of element from the arrayList
Example
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 34 35 36 37 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListRemoveExample2 { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); arrayList1.add("JQuery"); arrayList1.add("JSP"); arrayList1.add("Spring"); // Print the ArrayList System.out.println("Printing the Elements of ArrayList"); for (String element : arrayList1) { System.out.println(element); } // Remove element = "JSP" // Only first occurenece of JSP will be removed arrayList1.remove("JSP"); System.out .println("***Removed Element 'JSP'. Now Printing the Elements of ArrayList"); for (String element : arrayList1) { System.out.println(element); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 | Printing the Elements of ArrayList Java JSP JavaScript JQuery JSP Spring ***Removed Element 'JSP'. Now Printing the Elements of ArrayList Java JavaScript JQuery JSP Spring |
3. boolean removeAll(Collection<?> c)
Removes list of element from the ArrayList that are available in the collection.
Example
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 34 35 36 37 38 39 40 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListRemoveExample { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); arrayList1.add("JQuery"); arrayList1.add("JSP"); arrayList1.add("Spring"); // Print the ArrayList System.out.println("Printing the Elements of ArrayList"); for (String element : arrayList1) { System.out.println(element); } List<String> arrayList2 = new ArrayList<>(); arrayList2.add("Java"); arrayList2.add("JSP"); // Remove elements from List 1 that are available in List2 arrayList1.removeAll(arrayList2); System.out .println("***Removed Elements. Now Printing the Elements of ArrayList"); for (String element : arrayList1) { System.out.println(element); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Printing the Elements of ArrayList Java JSP JavaScript JQuery JSP Spring ***Removed Elements. Now Printing the Elements of ArrayList JavaScript JQuery Spring |