Java ConcurrentLinkedQueue add() methodThe add() method of ConcurrentLinkedQueue class inserts the specified element at the tail of this ConcurrentLinkedQueue. The addAll() method overrides the addAll in class AbstractQueue<E> SyntaxParametere- It is the element to add Specified ByThe add() method of ConcurrentLinkedQueue class is specified by :
Return ValueThe add() method returns a Boolean value true. ThrowsNullPointerException: If the specified element e is null. Example 1Test it NowOutput: After adding 1 to the queue : [1] After adding 2 to the queue : [1, 2] After adding 3 to the queue : [1, 2, 3] After adding 4 to the queue : [1, 2, 3, 4] After adding 5 to the queue : [1, 2, 3, 4, 5] Example 2Test it NowOutput: Queue : 718 8 18 119 Max number = 718 Min number = 8 Example 3Test it NowOutput: 1. Name = Reema 2. Name = Jyoti 3. Name = Rahul 4. Name = Anushtha Example 4Test it NowOutput: Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920) at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327) at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297) at com.javaTpoint.ConcurrentLinkedQueueAddExample4.main(ConcurrentLinkedQueueAddExample4.java:7) If any of the specified elements in this queue is null, it will give NullPointerException as described above.
Next TopicJava ConcurrentLinkedQueue
|