DelayQueue method retainAll() in Java
A DelayQueue method retainAll() retains only the elements in this collection that are contained in the specified collection or it removes all other elements from this deque that not contained in the specified collection. It returns boolean value true, if this deque changed, otherwise false.
Syntax
 1 public boolean retainAll(Collection<?> c)
c : It is a collection containing an element to be retained in this deque.
return : It returns boolean value true, if this deque changed, otherwise false.

Exceptions:

1. UnsupportedOperationException : if the retainAll operation is not supported by this collection2. ClassCastException : if the types of one or more elements in this collection are incompatible with the specified collection (optional)3. NullPointerException : if this collection contains one or more null elements and the specified collection does not permit null elements (optional), or if the specified collection is null
DelayQueue method retainAll()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.DelayQueue;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         DelayQueue<DelayInteger> deque = new DelayQueue<>();
 11         DelayInteger di1 = new DelayInteger(120, 1);
 12         DelayInteger di2 = new DelayInteger(140, 2);
 13         DelayInteger di3 = new DelayInteger(160, 3);
 14 
 15         deque.addAll(Arrays.asList(di1, di2));
 16         System.out.println("A DelayQueue elements : " + deque);
 17 
 18         // retain all elements in this deque, if present
 19         boolean res = deque.retainAll(Arrays.asList(di1, di3));
 20         System.out.println("Does this deque changed : " + res);
 21         System.out.println("A DelayQueue elements : " + deque);
 22     }
 23 }
In the above example, a DelayQueue of type DelayInteger created and added elements using addAll() method by passing a collection. A retainAll() method called by passing a collection that retains all specified elements in the deque, if present and remove all other elements. It returns boolean value true, as this deque changed. It compares elements using DelayInteger class equals() method.
Output
 1 A DelayQueue elements : [120 -> 1723707421984, 140 -> 1723707421985]
 2 Does this deque changed : true
 3 A DelayQueue elements : [120 -> 1723707421984]
DelayInteger class
 1 package com.java;
 2 
 3 import java.util.concurrent.Delayed;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 public class DelayInteger implements Delayed {
 7     private Integer value;
 8     private Long time;
 9 
 10     DelayInteger(Integer value, long time) {
 11         this.value = value;
 12         this.time = System.currentTimeMillis() + time;
 13     }
 14 
 15     @Override
 16     public long getDelay(TimeUnit unit) {
 17         long diff = time - System.currentTimeMillis();
 18         return unit.convert(diff, TimeUnit.SECONDS);
 19     }
 20 
 21     @Override
 22     public int compareTo(Delayed o) {
 23         Long val = ((DelayInteger)o).time;
 24         if(val == null || this.time == null) {
 25             return 0;
 26         } else {
 27             return (int)(this.time - val);
 28         }
 29     }
 30 
 31     @Override
 32     public String toString() {
 33         return this.value + " -> " + this.time;
 34     }
 35 }

DelayQueue method retainAll() with String

DelayQueue method retainAll() with String
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.DelayQueue;
 5 
 6 public class StringExp {
 7 
 8     public static void main(String[] args) {
 9 
 10         DelayQueue<DelayString> deque = new DelayQueue<>();
 11         DelayString ds1 = new DelayString("Welcome", 1);
 12         DelayString ds2 = new DelayString("to", 2);
 13         DelayString ds3 = new DelayString("IOGyan", 3);
 14 
 15         deque.addAll(Arrays.asList(ds1, ds2));
 16         System.out.println("A DelayQueue elements : " + deque);
 17 
 18         // retain all elements in this deque, if present
 19         boolean res = deque.retainAll(Arrays.asList(ds2, ds3));
 20         System.out.println("Does this deque changed : " + res);
 21         System.out.println("A DelayQueue elements : " + deque);
 22     }
 23 }
In the above example, a DelayQueue of type DelayString created and added elements using addAll() method by passing a collection. A retainAll() method called by passing a collection that retains all specified objects in the deque, if present and remove all other DelayString objects. It returns boolean value true, as this deque changed. It compares elements using DelayString class equals() method.
Output
 1 A DelayQueue elements : [Welcome -> 1723708251975, to -> 1723708251977]
 2 Does this deque changed : true
 3 A DelayQueue elements : [to -> 1723708251977]
DelayString class
 1 package com.java;
 2 
 3 import java.util.concurrent.Delayed;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 public class DelayString implements Delayed {
 7     private String name;
 8     private Long time;
 9 
 10     DelayString(String name, long time) {
 11         this.name = name;
 12         this.time = System.currentTimeMillis() + time;
 13     }
 14 
 15     @Override
 16     public long getDelay(TimeUnit unit) {
 17         long diff = time - System.currentTimeMillis();
 18         return unit.convert(diff, TimeUnit.SECONDS);
 19     }
 20 
 21     @Override
 22     public int compareTo(Delayed o) {
 23         Long val = ((DelayString)o).time;
 24         if(val == null || this.time == null) {
 25             return 0;
 26         } else {
 27             return (int)(this.time - val);
 28         }
 29     }
 30 
 31     @Override
 32     public String toString() {
 33         return this.name + " -> " + this.time;
 34     }
 35 }

DelayQueue method retainAll() with custom object

DelayQueue method retainAll() with custom object
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.DelayQueue;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         Product prd1 = new Product(1, "Phone", 2, 50000);
 11         Product prd2 = new Product(2, "Monitor", 2, 23000);
 12         Product prd3 = new Product(3, "Laptop", 2, 130000);
 13 
 14         DelayProduct dp1 = new DelayProduct(prd1, 1);
 15         DelayProduct dp2 = new DelayProduct(prd2, 2);
 16         DelayProduct dp3 = new DelayProduct(prd3, 3);
 17 
 18         DelayQueue<DelayProduct> deque = new DelayQueue<>();
 19         deque.addAll(Arrays.asList(dp1, dp2));
 20         System.out.println("A DelayQueue elements : " + deque);
 21 
 22         // retain all elements in this deque, if present
 23         boolean res = deque.retainAll(Arrays.asList(dp2, dp3));
 24         System.out.println("Does this deque changed : " + res);
 25         System.out.println("A DelayQueue elements : " + deque);
 26     }
 27 }
In the above example, a DelayQueue of type DelayProduct created and added elements using addAll() method by passing a collection. A retainAll() method called by passing a collection that retains all specified DelayProduct objects in the deque, if present and remove all other DelayProduct objects. It returns boolean value true, as this deque changed. It compares elements using DelayProduct class equals() method.
Output
 1 A DelayQueue elements : [Phone 2 50000.0 -> 1723708345371, Monitor 2 23000.0 -> 1723708345372]
 2 Does this deque changed : true
 3 A DelayQueue elements : [Monitor 2 23000.0 -> 1723708345372]
DelayProduct class
 1 package com.java;
 2 
 3 import java.util.concurrent.Delayed;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 public class DelayProduct implements Delayed {
 7     private Product product;
 8     private Long time;
 9 
 10     DelayProduct(Product product, long time) {
 11         this.product = product;
 12         this.time = System.currentTimeMillis() + time;
 13     }
 14 
 15     @Override
 16     public long getDelay(TimeUnit unit) {
 17         long diff = time - System.currentTimeMillis();
 18         return unit.convert(diff, TimeUnit.SECONDS);
 19     }
 20 
 21     @Override
 22     public int compareTo(Delayed o) {
 23         Long val = ((DelayProduct)o).time;
 24         if(val == null || this.time == null) {
 25             return 0;
 26         } else {
 27             return (int)(this.time - val);
 28         }
 29     }
 30 
 31     @Override
 32     public String toString() {
 33         return this.product + " -> " + this.time;
 34     }
 35 }

Product class

Product class
 1 package com.java;
 2 
 3 public class Product {
 4     private final int pid;
 5     private final String name;
 6     private final long quantity;
 7 
 8     private final double price;
 9 
 10     public Product(int pid, String name, long quantity, double price) {
 11         this.pid = pid;
 12         this.name = name;
 13         this.quantity = quantity;
 14         this.price = price;
 15     }
 16 
 17     public int getPid() {
 18         return pid;
 19     }
 20 
 21     public String getName() {
 22         return name;
 23     }
 24 
 25     public long getQuantity() {
 26         return quantity;
 27     }
 28 
 29     public double getPrice() { return price; }
 30 
 31     @Override
 32     public String toString() {
 33         return this.name + " " + this.quantity + " " + this.price;
 34     }
 35 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us