Collection Queue Interface | Key Methods and Classes

Collection Queue Interface

The Collection Queue interface is a sub-interface of the Collection interface in the Java Collection Framework. It represents a collection that follows the First-In-First-Out (FIFO) ordering principle, where elements are added to the end of the queue and removed from the front.

Interface Hierarchy

The Collection Queue interface is structured as follows:

                        +----------------+
                        |    Collection  |
                        +-------+--------+
                                |
                        +-------+--------+
                        |    Queue       |
                        +-------+--------+
                                |
                        +-------+--------+
                        |    Deque       |
                        +-------+--------+

The Queue interface extends the Collection interface and adds additional methods specific to queues. It inherits these methods from the Deque interface, which extends the Queue interface and provides more functionality for handling double-ended queues.

Key Methods

The Collection Queue interface includes several important methods for queue operations:

  • boolean add(E element): Adds the specified element to the end of the queue. If the queue is full, an exception is thrown.
  • boolean offer(E element): Adds the specified element to the end of the queue. Returns true if the element was successfully added, or false if the queue is full.
  • E remove(): Removes and returns the element at the front of the queue. If the queue is empty, an exception is thrown.
  • E poll(): Removes and returns the element at the front of the queue. Returns null if the queue is empty.
  • E element(): Returns the element at the front of the queue without removing it. If the queue is empty, an exception is thrown.
  • E peek(): Returns the element at the front of the queue without removing it. Returns null if the queue is empty.

Implementing Classes

The Collection Queue interface is implemented by various classes in the Java Collection Framework. Some of the commonly used implementations are:

  • LinkedList: Implements the Queue interface using a doubly-linked list.
  • PriorityQueue: Implements the Queue interface using a priority heap.
  • ArrayDeque: Implements the Deque interface using a resizable array.

Usage Example

Here's an example demonstrating the basic usage of the Collection Queue interface:

Queue<String> queue = new LinkedList<>();

queue.add("Apple");
queue.add("Banana");
queue.add("Orange");

String front = queue.peek();
System.out.println("Front element: " + front);

String removed = queue.poll();
System.out.println("Removed element: " + removed);

System.out.println("Updated queue: " + queue);

This code creates a queue using the LinkedList class, adds elements to the queue, retrieves the front element using peek(), removes an element using poll(), and displays the updated queue.

Conclusion

The Collection Queue interface provides a set of methods for working with queues in the Java Collection Framework. It offers functionality for adding, removing, and accessing elements in a FIFO manner. By implementing this interface, developers can utilize different queue implementations to suit their specific requirements.

I hope this information helps! Let me know if you have any further questions.

List      Set      Queue      Map     Collection     HashTable

Post a Comment

Previous Post Next Post