[Solved] Java Remove ArrayList by Value [closed]


Assuming your ArrayList is this:

List<String[]> arrayList = new ArrayList<>();
arrayList.add(new String[]{"R111","Red","50000"});
arrayList.add(new String[]{"R123","Blue","50000"});

you can do something like:

for (Iterator<String[]> iterator = arrayList.iterator();iterator.hasNext();) {
    String[] stringArray = iterator.next();
    if("R111".equals(stringArray[0])) {
        iterator.remove();
    }
}

You can safely remove an element using iterator.remove() while iterating the ArrayList. Also see The collection Interface.

An alternative shorter approach using Streams would be:

Optional<String[]> array = arrayList.stream().filter(a -> "R111".equals(a[0])).findFirst();
array.ifPresent(strings -> arrayList.remove(strings));

solved Java Remove ArrayList by Value [closed]