Java ArrayList remove element example shows how to remove an element from ArrayList in Java. The example also shows how to remove all elements or specific elements from ArrayList.
How to remove an element from ArrayList?
To remove an element from the ArrayList, use the remove
method.
1 |
public E remove(int index) |
This method removes an element at specified index from ArrayList. It shifts subsequent elements to the left. The remove
method returns the element which was removed 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 23 24 25 26 27 28 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListRemoveExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //add some elements aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("Four"); /* * To remove element from ArrayList * use remove method */ aListNumbers.remove(0); System.out.println( aListNumbers ); } } |
Output
1 |
[Two, Three, Four] |
You can also use an overloaded remove
method which accepts the object to be removed instead of the index.
1 |
public boolean remove(Object o) |
This method removes the first occurrence of the specified object from the ArrayList. This method returns true if the element was found and removed from the ArrayList, false otherwise.
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 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListRemoveExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //add some elements aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("Four"); /* * To remove specific object from ArrayList * use remove method */ aListNumbers.remove("Two"); System.out.println( aListNumbers ); } } |
Output
1 |
[One, Three, Four] |
How to remove objects from ArrayList?
Let’s try to remove objects 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListRemoveExample { public static void main(String[] args) { ArrayList<Emp> aListEmp = new ArrayList<Emp>(); aListEmp.add(new Emp("John", "M")); aListEmp.add(new Emp("Martin", "C")); aListEmp.add(new Emp("Maria", "S")); aListEmp.remove(new Emp("Martin", "C")); System.out.println( aListEmp ); } } class Emp{ private String firstName; private String lastName; public Emp(){} public Emp(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String toString(){ return firstName + " " + lastName; } } |
Output
1 |
[John M, Martin C, Maria S] |
As you can see from the output, the object is not removed from the ArrayList. The remove
method compares the objects to find the object to remove using equals
method and Emp class has not implemented equals
and hashcode
methods. Let’s implement these methods and try again.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListRemoveExample { public static void main(String[] args) { ArrayList<Emp> aListEmp = new ArrayList<Emp>(); aListEmp.add(new Emp("John", "M")); aListEmp.add(new Emp("Martin", "C")); aListEmp.add(new Emp("Maria", "S")); aListEmp.remove(new Emp("Martin", "C")); System.out.println( aListEmp ); } } class Emp{ private String firstName; private String lastName; public Emp(){} public Emp(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String toString(){ return firstName + " " + lastName; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Emp other = (Emp) obj; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; return true; } } |
Output
1 |
[John M, Maria S] |
How to remove all elements from the ArrayList?
You can use the clear
method of the ArrayList class to remove all elements 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 23 24 25 26 27 28 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListRemoveExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //add some elements aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("Four"); /* * To remove all elements from ArrayList * use clear method */ aListNumbers.clear(); System.out.println("ArrayList contains " + aListNumbers.size() + " elements"); } } |
Output
1 |
ArrayList contains 0 elements |
The ArrayList becomes empty after this method call. You can also use removeAll
method to remove all elements from the ArrayList as given below.
1 |
aListNumbers.removeAll(aListNumbers); |
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.