Java Collection Framework - AbstractQueue

Java Collection Framework - AbstractQueue


Posted in : Core Java Posted on : May 28, 2011 at 3:45 PM Comments : [ 0 ]

AbstractQueue class is an abstract class which extends the class AbstractCollection and implements the Queue interface. This class is a part of Java collection framework and is available in java.util package.

Java Collection Framework - AbstractQueue

AbstractQueue class is an abstract class which extends the class AbstractCollection and implements the Queue interface. This class is a part of Java collection framework and is available in java.util package. AbstractQueue provides the implementations of few Queue operations these implementations are suitable when the null elements are not allowed by the core implementation.

Syntax

public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E>

Constructor of AbstractQueue

AbstractQueue() : This constructor is used by the further subclasses.

Methods of AbstractQueue

This class provides the following methods :

  1. add() 
  2.                         syntax : public boolean add(E e)

  3. clear()  
  4.                         syntax : public void clear()

  5. addAll() 
  6.                         syntax : public boolean addAll(Collection<? extends E> c)

  7. element() 
  8.                         syntax : public E element()

  9. remove()
  10.                         syntax : public E remove()

Example

package devmanuals.com;

import java.util.AbstractQueue;

import java.util.Iterator;

import java.util.Queue;

import java.util.LinkedList;

public class AbstractQueueDemo extends AbstractQueue{

  Queue q;

  AbstractQueueDemo(){

  q=new LinkedList();

  }
  public boolean offer() {

    System.out.println(q);

    return true;

  }
  public boolean offer(Object e) {

    q.offer(e);

    return true;

  }
  public Iterator iterator() {

    Iterator itr = q.iterator();

    while(itr.hasNext()){

      System.out.println(itr.next());

    }

    return null;

  }
  public int size() {

    System.out.println(q.size());

    return 0;

  }  
  public Object peek() {

    Object o= q.peek();

    System.out.println(o);

    return null;

  }
  public Object poll() {

    Object o = q.poll();

    System.out.println(o);

    return null;

  }
  public static void main(String[] args) {

    AbstractQueueDemo aqd = new AbstractQueueDemo();

    System.out.println("Elements in queue are =");

    aqd.offer("A");

    aqd.offer("B");

    aqd.offer("C");

    aqd.offer("D");

    aqd.offer("E");

    aqd.offer("F");

    aqd.offer("G");

    aqd.offer();

    System.out.println("Displaying elements of queue using iterator =");

    aqd.iterator();

    System.out.println("Size of Queue =");

    aqd.size();

    System.out.println("The head element of Queue =");

    aqd.peek();

    System.out.println("The removed head element =");

    aqd.poll();

    System.out.println("Remaining element of queue");

    aqd.offer();

    System.out.println("Now the size of queue =");

    aqd.size();

  }

}

Output :

Elements in queue are =

[A, B, C, D, E, F, G]

Displaying elements of queue using iterator =

A

B

C

D

E

F

G

Size of Queue =

7

The head element of Queue =

A

The removed head element =

A

Remaining element of queue

[B, C, D, E, F, G]

Now the size of queue =

6

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics