Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to filter an ArrayList in-place with Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
        
        // removeIf Method: It takes a lambda expression or a predicate as an argument. 
        // The predicate defines the condition for removal.
        // The removeIf method modifies the original list directly (In-place), 
        
        // Remove elements greater than 4
        numbers.removeIf(n -> n > 4);

        // Print the filtered list
        System.out.println(numbers); // Output: [1, 2, 3]
    }
}

    
/*
run:
           
[1, 2, 3, 4]
           
*/

 



answered Jul 13, 2025 by avibootz
...