Rahul Dabas
Rahul Dabas

Reputation: 726

Remove object from list that matched a specified criteria

I have a List of Pojo which has about 10000 objects.

I need to remove an object from this List where pojo.getAccountId() = provided String.

Is this possible to remove this object without having to iterate over the whole list? I have to do a lot of removals and so I don't want to iterate over the list.

Currently I am planning to create a hashmap<> from my list where key = pojo.getAccountId(). Using map i can do map.remove(key).

I would like to avoid this conversion process if possible at all.

Upvotes: 0

Views: 3690

Answers (4)

Pear
Pear

Reputation: 470

I like to use the CollectionUtils from the apache commons library. It has a filter method to which you need to pass a predicate.

public void filterList(List<MyObject> myList, String testString) {
    CollectionUtils.filter(myList, new Predicate<MyObject>() {
        @Override
        public boolean evaluate(MyObject myObject) {
            return myObject.getAccountId().equals(testString);
        }
    });
}

This removes from the list all the objects that do not match the condition described in the Predicate. If you want to do the opposite, you can change the condition, or you can use the filterInverse method. Nevertheless, of course, it implicitly uses a for loop, but it is hidden to you.

Apache commons : http://commons.apache.org/proper/commons-collections/

CollectionUtils : http://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/CollectionUtils.html

Hope it was helpfull.

EDIT

As also said by Narmer, if you use JDK 1.8, it is even easier. You can create a stream on your list and call the filter Method in the same way.

myList.stream()
    .filter(myObject -> myObject.getAccountId().equals(testString))
    .collect(Collectors.toList())

Upvotes: 1

lance-java
lance-java

Reputation: 27986

Don't store the data in a List, store in a java.util.Map instead (keyed by id). The remove() will be a hash lookup.

If you want to maintain order, use LinkedHashMap or TreeMap.

Upvotes: 2

Narmer
Narmer

Reputation: 1454

Whichever method you use will always iterate through the list to remove your element. The only thing you can do is shorten and prettify your code as much as possible.

Here is a Java 8 one liner:

 boolean removed = myList.removeIf(pojo -> pojo.getAccountId().equals(provided));

Upvotes: 7

Mike Samuel
Mike Samuel

Reputation: 120506

Not unless your List implementation is exotic List that knows more about its elements than any of the core library collection types.

Stock Lists only ever call their contents equals method when checking for containment or equality of two lists, hashCode when generating a hashCode for the list as a whole, and possibly their serialization hooks when the list is serialized.


Currently I am planning to create a HashMap<> from my list where key = pojo.getAccountId()

This sounds like a fine option. You might want to use a LinkedHashMap though to preserve the iteration ordering from the original list.

Upvotes: 0

Related Questions