DelayQueue method element() in Java
A DelayQueue method element() retrieves the head of this deque without removing. It differs from peek() method where this method throws an exception if this deque is empty. It returns a head element of this deque, if not empty.
Syntax
 1 public E element()
return : It returns a head element of this deque, if not empty.

Exception:

NoSuchElementException : if this deque is empty
DelayQueue method element()
 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, di3));
 16         System.out.println("A DelayQueue elements : " + deque);
 17 
 18         // retrieve head element
 19         DelayInteger res = (DelayInteger)deque.element();
 20         System.out.println("A head element of this deque : " + 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 element() method called that returns a head element of this deque without removing. A resulting element assigned to the variable and print.
Output
 1 A DelayQueue elements : [120 -> 1723692555683, 140 -> 1723692555684, 160 -> 1723692555685]
 2 A head element of this deque : 120 -> 1723692555683
 3 A DelayQueue elements : [120 -> 1723692555683, 140 -> 1723692555684, 160 -> 1723692555685]
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 element() with String

DelayQueue method element() 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, ds3));
 16         System.out.println("A DelayQueue elements : " + deque);
 17 
 18         // retrieve head element
 19         DelayString res = (DelayString)deque.element();
 20         System.out.println("A head element of this deque : " + 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 element() method called that returns a head element of this deque without removing. A resulting DelayString object assigned to the variable and print.
Output
 1 A DelayQueue elements : [Welcome -> 1723695511553, to -> 1723695511554, IOGyan -> 1723695511555]
 2 A head element of this deque : Welcome -> 1723695511553
 3 A DelayQueue elements : [Welcome -> 1723695511553, to -> 1723695511554, IOGyan -> 1723695511555]
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 element() with custom object

DelayQueue method element() 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(prd2, 3);
 17 
 18         DelayQueue<DelayProduct> deque = new DelayQueue<>();
 19         deque.addAll(Arrays.asList(dp1, dp2, dp3));
 20         System.out.println("A DelayQueue elements : " + deque);
 21 
 22         // retrieve head element
 23         DelayProduct res = (DelayProduct)deque.element();
 24         System.out.println("A head element of this deque : " + 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 element() method called that returns a head element of this deque without removing. A resulting DelayProduct object assigned to the variable and print. It uses DelayProduct class toString() method to print DelayProduct object.
Output
 1 A DelayQueue elements : [Phone 2 50000.0 -> 1723696451988, Monitor 2 23000.0 -> 1723696451989, Monitor 2 23000.0 -> 1723696451990]
 2 A head element of this deque : Phone 2 50000.0 -> 1723696451988
 3 A DelayQueue elements : [Phone 2 50000.0 -> 1723696451988, Monitor 2 23000.0 -> 1723696451989, Monitor 2 23000.0 -> 1723696451990]
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