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:
LinkedBlockingDeque retainAll() method in Java with Examples
Next article icon

LinkedBlockingDeque retainAll() method in Java with Examples

Last Updated : 19 Mar, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The retainAll() method of LinkedBlockingDeque is an in-built function is Java which is used to remove from this deque all of its elements that are contained in the specified collection. That means, all the common elements these two collections are removed from this deque. Syntax:
  public boolean retainAll(Collection c)  
Parameters: This method takes collection c as a parameter containing elements to be removed from this list. Return Value: This method returns true if this deque changed as a result of the call. Exceptions: This method throws NULL Pointer Exception if the specified collection is Null. Below program illustrates the retainAll() function of LinkedBlockingDeque class: Example 1: Java
// Java Program Demonstrate retainAll() // method of LinkedBlockingDeque  import java.util.concurrent.LinkedBlockingDeque; import java.util.*;  public class GFG {     public static void main(String[] args)         throws IllegalStateException     {          // Create object of LinkedBlockingDeque         LinkedBlockingDeque<Integer> LBD             = new LinkedBlockingDeque<Integer>();          // Add numbers to end of LinkedBlockingDeque         LBD.add(11);         LBD.add(22);         LBD.add(33);         LBD.add(44);          // print deque         System.out.println("Linked Blocking Deque: "                            + LBD);          // Create object of ArrayList collection         ArrayList<Integer> ArrLis             = new ArrayList<Integer>();          // Add number to ArrayList         ArrLis.add(55);         ArrLis.add(11);         ArrLis.add(23);         ArrLis.add(22);          // Print ArrayList         System.out.println("ArrayList to be retained: "                            + ArrLis);          // Function retainAll() retains         // all the common elements from deque         LBD.retainAll(ArrLis);          // Print deque         System.out.println("Retained Linked Blocking Deque: "                            + LBD);     } } 
Output:
  Linked Blocking Deque: [11, 22, 33, 44]  ArrayList to be retained: [55, 11, 23, 22]  Retained Linked Blocking Deque: [11, 22]  
Example 2: Java
// Java Program Demonstrate retainAll() // method of LinkedBlockingDeque  import java.util.concurrent.LinkedBlockingDeque; import java.util.*;  public class GFG {     public static void main(String[] args)         throws IllegalStateException     {          // Create object of LinkedBlockingDeque         LinkedBlockingDeque<String> LBD             = new LinkedBlockingDeque<String>();          // Add numbers to end of LinkedBlockingDeque         LBD.add("geeks");         LBD.add("Geeks");         LBD.add("gfg");         LBD.add("Gfg");          // Print deque         System.out.println("Linked Blocking Deque: "                            + LBD);          // Create object of ArrayList collection         ArrayList<String> ArrLis             = new ArrayList<String>();          // Add elements to ArrayList         ArrLis.add("GeeksforGeeks");         ArrLis.add("Geeks");         ArrLis.add("gfg");         ArrLis.add("Gfg");          // Print ArrayList         System.out.println("ArrayList to be retained: "                            + ArrLis);          // Function retainAll() retains         // all the common elements from deque         LBD.retainAll(ArrLis);          // Print deque         System.out.println("Retained Linked Blocking Deque: "                            + LBD);     } } 
Output:
  Linked Blocking Deque: [geeks, Geeks, gfg, Gfg]  ArrayList to be retained: [GeeksforGeeks, Geeks, gfg, Gfg]  Retained Linked Blocking Deque: [Geeks, gfg, Gfg]  
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedBlockingDeque.html#retainAll-java.util.Collection-

Next Article
LinkedBlockingDeque retainAll() method in Java with Examples

P

ProgrammerAnvesh
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Functions
  • Java-LinkedBlockingDeque
  • Java-Classes
Practice Tags :
  • Java

Similar Reads

    LinkedBlockingDeque removeAll() method in Java with Examples
    The removeAll() method of LinkedBlockingDeque is an in-built function is Java which is used to remove from this deque all of its elements that are contained in the specified collection. That means, all the common elements these two collections are removed from this deque.Syntax: public boolean remov
    2 min read
    LinkedBlockingDeque removeIf() method in Java with Examples
    The removeIf() method of LinkedBlockingDeque removes the element from this LinkedBlockingDeque that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<? super E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based o
    2 min read
    LinkedBlockingQueue take() Method in Java with Examples
    The take() method of LinkedBlockingQueue is used to retrieve and remove the head of this queue. If the queue is empty then it will wait until an element becomes available. This method is more efficient if working on threads and using LinkedBlockingQueue in that process. So the thread that initially
    4 min read
    LinkedBlockingQueue put() method in Java with Examples
    The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockin
    3 min read
    LinkedTransferQueue retainAll() method in Java with Examples
    The retainAll() method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to retain all the elements in this queue which are common to both i.e. the specified collection and this queue. All other elements which are not common are removed from this queue. Syntax
    3 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