LinkedBlockingQueue clear() method in Java
Last Updated :
26 Nov, 2018
The clear() method of LinkedBlockingQueue removes all of the elements from this queue. After applying this method the queue will become empty.
Syntax:
public void clear()
Below programs illustrates clear() method of LinkedBlockingQueue class:
Program 1:
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 50 ;
LinkedBlockingQueue<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
linkedQueue.add( 2300 );
linkedQueue.add( 1322 );
linkedQueue.add( 8945 );
linkedQueue.add( 6512 );
System.out.println( "LinkedBlockingQueue before using"
+ " clear() : " + linkedQueue);
linkedQueue.clear();
System.out.println();
System.out.println( "LinkedBlockingQueue after using"
+ " clear() method : " + linkedQueue);
}
}
|
Output:
LinkedBlockingQueue before using clear() : [2300, 1322, 8945, 6512]
LinkedBlockingQueue after using clear() method : []
Program 2:
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 50 ;
LinkedBlockingQueue<String> names
= new LinkedBlockingQueue<String>(capacityOfQueue);
names.add( "Shubham" );
names.add( "Siddhant" );
names.add( "Mahafuj" );
names.add( "Arka" );
names.add( "Raunak" );
System.out.println( "LinkedBlockingQueue before using"
+ " clear() : " + names);
names.clear();
System.out.println();
System.out.println( "LinkedBlockingQueue after using"
+ " clear() method : " + names);
}
}
|
Output:
LinkedBlockingQueue before using clear() : [Shubham, Siddhant, Mahafuj, Arka, Raunak]
LinkedBlockingQueue after using clear() method : []
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#clear–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...