Example 1:
The program is given to demonstrate the usage of the Java remove() method. The remove() method takes the index value as an argument of the array list to eliminate the element which is placed at the specified index.
class RemoveDemo1 {
public static void main(String[] args) {
List num = new ArrayList();
num.add(9);
num.add(19);
num.add(29);
num.add(39);
num.add(49);
num.add(59);
System.out.println(num);
num.remove(1);
num.remove(3);
System.out.println(num);
}
}
The list class is specified with the integer generic type in the previous illustration. We create the “num” list attribute and initialize it with the ArrayList() to create the list of an array. As the array list is empty, we have to insert the value to fill it. We use the add() function to add the integer values in the specified list. After that, we display the filled array of lists.
Next, we deploy the remove() method which is called by the “num” list attribute because this remove() method is applied to it. The remove() method is provided with the “1” and “3” index values which discard the numbers that are placed at the index “1” and “3” in the array of the numeric list. Again, we display the “num” array of list for the remaining list item.
The numeric array list is generated which has six elements inside it. After the removed operation, the index “1” and “3” are removed and the remaining list item is displayed in the console.
Example 2:
In the aforementioned remove() method program, the remove() method is given an index value as a parameter to remove that index from the list. The remove() method also takes the element of the list as an argument to delete that specific element from the given list.
public class RemoveDemo2 {
public static void main(String[] args) {
ArrayList languages = new ArrayList(5);
Languages.add("python");
languages.add("java");
languages.add("Kotlin");
languages.add("Scala");
System.out.println("List size is: " + languages.size());
for (String str : languages) {
System.out.println("Name is: + str);
}
Languages.remove("python");
System.out.println("\nAfter removing the element the size is: languages.size());
for (String str : languages) {
System.out.println("String is: + str);
}
}
}
The ArrayList string is created with the default value of “5” in the “languages” variable. The ArrayList is initially empty which is updated by adding the string value using the add() function. As the ArrayList size is fixed, we cannot add the elements inside it more than “5”. We check the size of the ArrayList by invoking the size() method with the “languages” variable. After this, the names of various programming languages are printed from our ArrayList. programming languages. Then, we generate the remove() method which is called by the ArrayList “languages” variable. The remove() method is specified with the string language name “Python” to be removed from the ArrayList. After that particular string element removal from the list, we get the size of the ArrayList again by deploying the size() method. The altered string list is then printed by iterating it over the for-loop.
The ArrayList size along with the elements is shown on the screen. The ArrayList after removing the element with the size is also shown:
Example 3:
The removeIf() method is more appropriate in the condition when we would like to remove a value from the ArrayList that matches the predicate filter. The predicate filter is passed as an argument to that method.
public static void main(String[] args) {
ArrayListarr = new ArrayList(5);
arr.add("Emily");
arr.add("Billy");
arr.add("Jack");
arr.add("James");
System.out.println("Size is: + arr.size());
for (String n :arr) {
System.out.println("Array Elements: " + n);
}
arr.removeIf(x -> (x.charAt(0)== 'J'));
System.out.println("\nNow Size is: "+ arr.size());
for (String n :arr) {
System.out.println("Elements:+arr);
}
}
}
The “arr” variable of the ArrayList is declared in the program to generate the ArrayList which only inputs the five values since the size is already defined. The values are added to the “arr” ArrayList within the range. Then, to print every value from the ArrayList, we invoke the foreach method. After that, we apply the removeIf() method on the “arr” ArrayList. The predicate is defined to removeIf() as a parameter. The removeIf() method gets through each character of the specified string in the ArrayList and removes the string value from that list that begins with the character value of “J”. Then, we get the size of the “arr” ArrayList after this operation. The ArrayList is then traversed by the foreach method to print the ArrayList after the removeIf() operation.
The output of the previous program first generates the size of the ArrayList and its elements. Then, it outputs the ArrayList with the updated size and elements after the removeIf() method.
Example 4:
The remove() method cases are explored in the prior programs. There is a clear() method that works similarly to the remove() method. It also removes or clears the items from the ArrayList.
The ArrayList() method is called in the “ArrList” variable along with the fixed size. By keeping the size of the ArrayList in mind, we add the elements of string type in our “ArrList” ArrayList. After that, we utilize the clear() method on the “ArrList” to clear out the entire “ArrList”. Then, we print the “ArrList” which is retrieved after the clear() method.
The ArrayList which is generated in the following is empty since the clear() method clears all of the entries from the ArrayList.
Example 5:
Although, the list elements that are included in the provided collection are also removed using the removeAll() method of the ArrayList class just like the Java clear() method.
class RemoveDemo5 {
public static void main(String[] args) {
ArrayList players = new ArrayList();
players.add("Smith");
players.add("Milo");
players.add("Archie");
System.out.println("ArrayList is: + players);
players.removeAll(players);
System.out.println("After removeAll() ArrayList: + players);
}
}
The program is declared with the “players” variable of the ArrayList. We set that “players” variable with the empty ArrayList that can only add the string values. The string values are then inserted in the ArrayList by the add() function. The initial ArrayList is printed before the removeAll() method. After that, we employ the removeAll() method with the “players” ArrayList. The removeAll() method passes with the “players” variable to remove all the items that exist in the ArrayList. The ArrayList is then printed to see the before and after results of the removeAll() method.
The ArrayList of items before the removeAll() method returns all the items. But after the removeAll() method, the ArrayList that is generated in the following is empty:
Conclusion
The remove() method of the ArrayList eliminates the element’s first occurrence from the specified list if that element is present. The list is not changed in the case when the elements are not located in the list. We explained to remove the elements from the list using the remove() method with the appropriate example. Additionally, we also provided the removeIf() method, removeAll() method, and clear() method which also helps to remove the list elements.