Java ArrayList removeif() Function
What is an ArrayList?
Here, in the page we will discuss about the ArrayList’s removeIf() Method in java.
Arraylist is a part of collection framework in java. It contains some inbuilt function like removeIf() function which can be found in the java.util.ArrayList Package.It implements the List interface of the collections framework.
ArrayList removeIf Function:
Arraylist contains several inbuilt functions that are used to perform several operations on arraylist in Java. removeIf function is also an inbuilt method of class ArrayList.
removeIf function is used to remove all the elements of an ArrayList that satisfy the specified condition of the function.
Syntax:
arraylist_name.removeIf(Predicate filter)
Definition of Parameters:
Return Type :
Exceptions:
clear() Function in ArrayList
clone() Function in ArrayList
add() Function in ArrayList
addall() Function in ArrayList
Example for Removing Elements From ArrayList of Integers:
import java.util.*; public class Main{ public static void main(String[] args){ // Initializing the new Arraylist ArrayList arr = new ArrayList<>(); // Adding Elements to the ArrayList arr.add(1); arr.add(2); arr.add(3); arr.add(4); arr.add(5); arr.add(6); // Printing the ArrayList System.out.println("Arraylist arr : " + arr); // Invoking the removeif function arr.removeIf(Prep -> (Prep % 2 != 0)); // Printing the new ArrayList System.out.println("After removing odd Elements : " + arr); } }
Output:
Arraylist arr : [1, 2, 3, 4, 5, 6] After removing odd Elements : [2, 4, 6]
Explanation:
In the above code, we have created an arraylist arr containing some integers and after invoking the removeIf() method with condition of removing the odd numbers from the ArrayList “arr”. It will remove all its odd elements from the list.
Example for Removing Elements of ArrayList of String Type:
import java.util.*; public class Main{ public static void main(String[] args){ // Initializing the new ArrayList ArrayList arr = new ArrayList<>(); // Adding elements to the ArrayList arr.add("Java"); arr.add("C++"); arr.add("C"); arr.add("Python"); arr.add(".net"); // Printing the Arraylist System.out.println("Arraylist arr : " + arr); // Invoking the removeif Function arr.removeIf(Prep -> (Prep.charAt(0) == 'C')); // Printing the new ArrayList System.out.println("After removing odd Elements : " + arr); } }
Output:
Arraylist arr : [Java, C++, C, Python, .net] After removing odd Elements : [Java, Python, .net]
Explanation:
In the above Example. we add 5 elements of String type to the ArrayList “arr” with condition of string starting with character ‘c. It will remove all the elements that are starting with character ‘c’.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment