Removeif arraylist java пример

Remove Element from an ArrayList in Java

Learn to remove an item from an ArrayList using the remove() and removeIf() methods. The remove() method removes either the specified element or an element from the specified index. The removeIf() removes all elements matching a Predicate.

The ArrayList.remove() method is overloaded.

E remove(int index) boolean remove(E element))
  • The remove(int index) – removes the element at the specified index and returns the removed item.
  • remove(E element) – remove the specified element by value and returns true if the element was removed, else returns false.

The removeAll() accepts a collection and removes all elements of the specified collection from the current list.

boolean removeAll(Collection c)

The removeIf() accepts a Predicate to match the elements to remove.

boolean removeIf(Predicate filter)

2. Remove an Element by Specified Index

The remove(index) method removes the specified element E at the specified position in this list. It removes the element currently at that position, and all subsequent elements are moved to the left (will subtract one from their indices).

Note that List Indices start with 0.

ArrayList namesList = new ArrayList(Arrays.asList( "alex", "brian", "charles") ); System.out.println(namesList); //list size is 3 //Remove element at 1 index namesList.remove(1); System.out.println(namesList); //list size is 2
[alex, brian, charles] [alex, charles]

2. Remove Specified Element(s) By Value

The remove(Object) method removes the first occurrence of the specified element E in this list. As this method removes the object, the list size decreases by one.

ArrayList namesList = new ArrayList<>(Arrays.asList( "alex", "brian", "charles", "alex") ); System.out.println(namesList); namesList.remove("alex"); System.out.println(namesList);
[alex, brian, charles, alex] [brian, charles, alex]

To remove all occurrences of the specified element, we can use the removeAll() method. As this method accepts a collection argument, we first need to wrap the element to remove inside a List.

namesList.removeAll(List.of("alex"));

3. Remove Element(s) with Matching Condition

Читайте также:  Меняем цвет шрифта при помощи HTML

We can use another super easy syntax from Java 8 stream to remove all elements for a given element value using the removeIf() method.

The following Java program uses List.removeIf() to remove multiple elements from the arraylist in java by element value.

ArrayList namesList = new ArrayList(Arrays.asList( "alex", "brian", "charles", "alex") ); System.out.println(namesList); namesList.removeIf( name -> name.equals("alex")); System.out.println(namesList);
[alex, brian, charles, alex] [brian, charles]

Источник

Java ArrayList.removeIf()

The ArrayList.removeIf() iterates the list and removes all of the elements of this ArrayList that satisfy the given Predicate.

The removeIf() takes a single argument of type Predicate. The Predicate interface is a functional interface that represents a condition (boolean-valued function) of one argument. It checks that is a given argument met the condition or not.

public boolean removeIf(Predicate filter);

Method parameter – filter predicate that returns true for elements to be removed.
Method returns – true if any elements were removed from this list.
Method throws – NullPointerException if predicate is null .

2. ArrayList.removeIf() Example

The following Java programs use removeIf() method to remove elements that match a Predicate.

2.1. Remove All Even Numbers

In this simple example, we have a list of odd/even numbers and remove all even numbers from the list.

ArrayList numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); numbers.removeIf(number -> number % 2 == 0); System.out.println(numbers);

2.2. Remove Objects by Field

In this simple example, we have a list of employees, and we are removing all employees whose names start with char ‘P’ . The predicate, employee.getName().startsWith(“P”) matches the name of each Employee and returns true if the name starts with P.

ArrayList employees = new ArrayList<>(); employees.add(new Employee(1l, "Alex", LocalDate.of(2018, Month.APRIL, 21))); employees.add(new Employee(4l, "Brian", LocalDate.of(2018, Month.APRIL, 22))); employees.add(new Employee(3l, "Piyush", LocalDate.of(2018, Month.APRIL, 25))); employees.add(new Employee(5l, "Charles", LocalDate.of(2018, Month.APRIL, 23))); employees.add(new Employee(2l, "Pawan", LocalDate.of(2018, Month.APRIL, 24))); Predicate condition = employee -> employee.getName().startsWith("P"); employees.removeIf(condition); System.out.println(employees);
[ Employee [id=1, name=Alex, dob=2018-04-21], Employee [id=4, name=Brian, dob=2018-04-22], Employee [id=5, name=Charles, dob=2018-04-23] ]

That’s all for the ArrayList removeIf() in Java.

Читайте также:  How to calculate javascript

Источник

How to remove element from ArrayList by checking its value?

I know we can iterate over arraylist, and .remove() method to remove element but I dont know how to do it while iterating. How can I remove element which has value «acbd», that is second element?

11 Answers 11

In your case, there’s no need to iterate through the list, because you know which object to delete. You have several options. First you can remove the object by index (so if you know, that the object is the second list element):

 a.remove(1); // indexes are zero-based 

Or, you can remove the first occurence of your string:

 a.remove("acbd"); // removes the first String object that is equal to the // String represented by this literal 

Or, remove all strings with a certain value:

It’s a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can’t remove them by using remove with an object that is equal to the one you want to delete.

In those case, I usually use a second list to collect all instances that I want to delete and remove them in a second pass:

 List deleteCandidates = new ArrayList<>(); List myBeans = getThemFromSomewhere(); // Pass 1 - collect delete candidates for (MyBean myBean : myBeans) < if (shallBeDeleted(myBean)) < deleteCandidates.add(myBean); >> // Pass 2 - delete for (MyBean deleteCandidate : deleteCandidates)

Источник

Оцените статью