
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between peek(), poll() and remove() method of Queue interface in java?
This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.
The peek() method
The peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.
Example
import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String args[]) { Queue<String> queue = new LinkedList<String>(); queue.add("Java"); queue.add("JavaFX"); queue.add("OpenCV"); queue.add("Coffee Script"); queue.add("HBase"); System.out.println("Element at the top of the queue: "+queue.peek()); Iterator<String> it = queue.iterator(); System.out.println("Contents of the queue: "); while(it.hasNext()) { System.out.println(it.next()); } } }
Output
Element at the top of the queue: Java Contents of the queue: Java JavaFX OpenCV Coffee Script Hbase
The poll() method
The poll() method of the Queue interface returns the object at the top of the current queue and removes it. If the queue is empty this method returns null.
Example
import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String args[]) { Queue<String> queue = new LinkedList<String>(); queue.add("Java"); queue.add("JavaFX"); queue.add("OpenCV"); queue.add("Coffee Script"); queue.add("HBase"); System.out.println("Element at the top of the queue: "+queue.poll()); Iterator<String> it = queue.iterator(); System.out.println("Contents of the queue: "); while(it.hasNext()) { System.out.println(it.next()); } } }
Output
Element at the top of the queue: Java Contents of the queue: JavaFX OpenCV Coffee Script HBase
- Related Articles
- Queue poll() method example in Java
- Difference between default and static interface method in Java 8.
- ArrayBlockingQueue poll() Method in Java
- Difference Between Priority Queue and Queue Implementation in Java?
- IntStream peek() method in Java
- LongStream peek() method in Java
- ArrayBlockingQueue peek() Method in Java
- DoubleStream peek() method in Java
- Difference Between Class and Interface in Java
- Difference between Abstract Class and Interface in Java
- Difference between Runnable and Callable interface in java
- Difference Between Iterator and Enumeration Interface in Java
- Difference Between Thread Class and Runnable Interface in Java
- Difference Between Interface and Abstract Class in Java & C#
- Difference Between Linear Queue and Circular Queue

Advertisements