How to extend the collections framework : Queue « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Queue 




How to extend the collections framework
  
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.AbstractQueue;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;

/**
 * This program demonstrates how to extend the collections framework.
 @version 1.20 2007-05-16
 @author Cay Horstmann
 */
public class CircularArrayQueueTest
{
   public static void main(String[] args)
   {
      Queue<String> q = new CircularArrayQueue<String>(5);
      q.add("Amy");
      q.add("Bob");
      q.add("Carl");
      q.add("Deedee");
      q.add("Emile");
      q.remove();
      q.add("Fifi");
      q.remove();
      for (String s : qSystem.out.println(s);
   }
}

/** 
    A first-in, first-out bounded collection. 
*/ 
class CircularArrayQueue<E> extends AbstractQueue<E>

   /** 
       Constructs an empty queue. 
       @param capacity the maximum capacity of the queue 
   */ 
   public CircularArrayQueue(int capacity
   
      elements = new Object[capacity]
      count = 0
      head = 0
      tail = 0
   

   public boolean offer(E newElement
   
      assert newElement != null;
      if (count < elements.length
      {
         elements[tail= newElement; 
         tail = (tail + 1% elements.length; 
         count++;
         modcount++;
         return true;
      }
      else 
         return false;
   

   public E poll() 
   
      if (count == 0return null;
      E r = peek()
      head = (head + 1% elements.length; 
      count--; 
      modcount++;
      return r; 
   

   @SuppressWarnings("unchecked")
   public E peek() 
   
      if (count == 0return null;
      return (Eelements[head]
   

   public int size() 
   
      return count; 
   

   public Iterator<E> iterator()
   {
      return new QueueIterator();
         
   }

   private class QueueIterator implements Iterator<E>
   {
      public QueueIterator()
      {
         modcountAtConstruction = modcount;
      }

      @SuppressWarnings("unchecked")
      public E next() 
      
         if (!hasNext()) throw new NoSuchElementException();
         E r = (Eelements[(head + offset% elements.length]
         offset++;
         return r;
      }

      public boolean hasNext() 
      
         if (modcount != modcountAtConstruction
            throw new ConcurrentModificationException();
         return offset < count;
      }

      public void remove() 
      
         throw new UnsupportedOperationException()
      }

      private int offset;
      private int modcountAtConstruction;
   }

   private Object[] elements; 
   private int head; 
   private int tail; 
   private int count; 
   private int modcount;
}

   
    
  














Related examples in the same category
1.Priority queuePriority queue
2.Queue data structureQueue data structure
3.Convert a Queue to a List
4.Create a queue using LinkedList class
5.Simple Queue (FIFO) based on LinkedList
6.The Generic Queue Class
7.Blocking Queue
8.Circular Queue
9.Circular Queue extends AbstractList
10.An unbounded {@link TransferQueue} based on linked nodes.
11.This class implements the data structures necessary for an ArrayQueue
12.A circular queue from mina
13.An unbounded TransferQueue based on linked nodes.
14.Rotating queue of fixed size.
15.Allows threads to communicate asynchronously by putting messages into and reading messages out of a synchronized queue.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.