Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java 8 | ArrayDeque removeIf() method in Java with Examples
Next article icon

Java 8 | ArrayDeque removeIf() method in Java with Examples

Last Updated : 10 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition checking function, which checks the given input for a given condition and returns a boolean result for the same indicating whether the condition was met or not. Syntax:
public boolean removeIf(Predicate<? super E> filter)
Parameter: This method takes a parameter filter which represents a predicate which returns true for elements to be removed. Returns: This method returns True if predicate returns true and some elements were removed. Exception: This method throws NullPointerException if the specified filter is null. Below programs illustrate removeIf() method of ArrayDeque: Example 1: To demonstrate removeIf() method on ArrayDeque which contains a set of String and remove strings starts with A. Java
// Java Program Demonstrate removeIf() // method of ArrayDeque  import java.util.*;  public class GFG {     public static void main(String[] args)     {          // create an ArrayDeque         // containing a list of string values         ArrayDeque<String> students = new ArrayDeque<String>();          // Add Strings to list         // each string represents student name         students.add("Aman");         students.add("Sanjeet");         students.add("Amar");         students.add("Rabi");         students.add("Shabbir");          // apply removeIf() method         // to remove names which start with A         students.removeIf(n -> (n.charAt(0) == 'A'));          System.out.println("Students name do not starts with A");          // print list         for (String str : students) {             System.out.println(str);         }     } } 
Output:
  Students name do not starts with A  Sanjeet  Rabi  Shabbir  
Example 2: To demonstrate removeIf() method on ArrayDeque which contains set of Students objects to remove all those students who got less than 40 marks. Java
// Java Program Demonstrate removeIf() // method of ArrayDeque  import java.util.*;  public class GFG {     public static void main(String[] args)     {          // create an ArrayDeque         // containing a list of Student objects         ArrayDeque<student> students = new ArrayDeque<student>();          // Add student object to list         students.add(new student("Aman", 78));         students.add(new student("Amar", 79));         students.add(new student("Suraj", 38));         students.add(new student("Raju", 22));         students.add(new student("Ankit", 76));         students.add(new student("Sanju", 62));          // apply removeIf() method         // to remove students who scores below 40         students.removeIf(n -> (n.marks <= 40));          System.out.println("Students list who score above 40");          // print list         for (student str : students) {             System.out.println(str.name + ", " + str.marks);         }     } }  // create student class class student {      public String name;     public int marks;      student(String name, int marks)     {         this.name = name;         this.marks = marks;     } } 
Output:
  Students list who score above 40  Aman, 78  Amar, 79  Ankit, 76  Sanju, 62  
Example 3: To demonstrate NullpointerException in removeIf() method. Java
// Java Program Demonstrate removeIf() // method of ArrayDeque  import java.util.*;  public class GFG {     public static void main(String[] args)     {          // create an ArrayDeque         // containing a list of string values         ArrayDeque<String> students = new ArrayDeque<String>();          // Add Strings to list         // each string represents student name         students.add("Aman");         students.add("Sanjeet");         students.add("Amar");         students.add("Rabi");         students.add("Shabbir");          try {             // apply removeIf() method with null filter             students.removeIf(null);         }         catch (Exception e) {             System.out.println("Exception: " + e);         }     } } 
Output:
  Exception: java.lang.NullPointerException  
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#removeIf(java.util.function.Predicate)

Next Article
Java 8 | ArrayDeque removeIf() method in Java with Examples

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-Functions
  • Java-ArrayDeque
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    ArrayList removeIf() Method in Java with Examples
    The Java ArrayList removeIf() method is used to remove all elements from the ArrayList that satisfy a given predicate filter. The predicate is passed as a parameter to the method, and any runtime exceptions thrown during iteration or by the predicate are passed to the caller.The removeIf() method us
    2 min read
    ArrayList removeAll() Method in Java with Examples
    The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself.Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argument
    3 min read
    CopyOnWriteArraySet removeIf() method in Java with Examples
    The removeIf() method of CopyonWriteArraySet method removes the element from this CopyOnWriteArraySet that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on
    2 min read
    BlockingDeque remove() method in Java with Examples
    The remove() method of BlockingDeque removes the head of the Deque container. The method throws a NoSuchElementException if the Deque container is empty. If an element in passed in the parameter, it removes the given element if present in the Deque. Syntax: public E remove() or boolean remove(elemen
    2 min read
    Map remove() Method in Java with Examples
    The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. Syntax: V remove(Object key)Parameters: This method has the only argument key, whose mapping is to be removed from the map.Returns: This method returns the value to which this map previously as
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences