I have an array list of 100 000 objects. I want to remove some of the objects which have a particular ID. I'm using for loop to search through the ArrayList and remove each of them. Hence it is very much time-consuming. Is there any possible way to do this without any looping?
            Asked
            
        
        
            Active
            
        
            Viewed 1,062 times
        
    -6
            
            
         
    
    
        James Z
        
- 12,209
- 10
- 24
- 44
 
    
    
        Akhil Aravind
        
- 9
- 6
- 
                    What have you tried so far and what Java version are you running? Java 8 or higher? – deHaar Aug 05 '19 at 10:20
- 
                    @deHaar, i have tried in Android with java 8 – Akhil Aravind Aug 05 '19 at 10:25
3 Answers
8
            List<Type> list = ...
list.removeIf(item -> item.getId() == something);
As the name suggests, removeIf() removes all elements if they satisfy the predicate.
 
    
    
        cameron1024
        
- 9,083
- 2
- 16
- 36
- 
                    
- 
                    
- 
                    
- 
                    Time complexity is linear. AFAIK impossible to do better than that. – cameron1024 Aug 05 '19 at 10:55
- 
                    
- 
                    Time complexity of `removeIf` is linear (so is `removeAll` with a set or small collection argument), but [as elaborated here](https://stackoverflow.com/a/57094996/2711488), removing objects in a loop, as needed when having to check the ID, is worse than linear. So, using `removeIf` *has* a performance benefit. – Holger Aug 05 '19 at 13:22
- 
                    @Seelenvirtuose compared to looping over the list and removing elements, it *has* better time complexity. – Holger Aug 05 '19 at 13:28
1
            
            
        Since I don't know the structure of your Lakh class, I can only provide an example using Strings:
public static void main(String args[]) throws Exception {
    List<String> words = new ArrayList<>();
    words.add("I");
    words.add("want");
    words.add("to");
    words.add("remove");
    words.add("things");
    words.add("without");
    words.add("a");
    words.add("loop");
    System.out.println("———— BEFORE ————");
    words.forEach(word -> System.out.println(word));
    System.out.println("———— AFTER ————");
    // this is the removal action, it removes all entries that equal certain words
    words.removeIf(word -> word.equals("loop") || word.equals("remove"));
    words.forEach(word -> System.out.println(word));
}
Please not that this will use a loop under the hood, too. More precise:
AnIterator<E>in awhileloop.
 
    
    
        deHaar
        
- 17,687
- 10
- 38
- 51
- 
                    @Adrian thanks, I wasn't able to extract that from the context *1 Lakh objects* but it's beginning to make sense ;-) – deHaar Aug 05 '19 at 12:17
- 
                    2The `default` implementation of `removeIf` uses an iterator based loop, but `ArrayList` overrides this method with a far better implementation, suited to the `ArrayList`. – Holger Aug 05 '19 at 13:24
-2
            
            
        You can use remove() method like below:
List<String> nameList = ArrayList<String>();
nameList.add("Sam");
nameList.add("Sarah");
//Remove by index
nameList.remove(0);
//Remove by value
nameList.remove("Sarah");
 
    
    
        Fathi-ch3k
        
- 1
- 1