Java中的AbstractQueue与示例
Java中的AbstractQueue类是Java Collection Framework 的一部分,实现了Collection接口和AbstractCollection类。它提供了一些Queue操作的骨架实现。当基本实现不允许空元素时,此类中的实现是合适的。 add、remove 和 element 方法分别基于 offer、poll 和 peek,但会抛出异常,而不是通过 false 或 null 返回来指示失败。
类层次结构:
java.lang.Object
↳ java.util.AbstractCollection
↳ Class AbstractQueue
此类实现Iterable
宣言:
public abstract class AbstractQueue
E –由集合框架类或接口维护的元素类型。
Java AbstractQueue 中的构造函数
由于 AbstractQueue 是一个抽象类,它的实现由它的子类提供。下面显示了可以提供实现的类列表。要创建它,我们需要从Java.util.AbstractQueue中创建它。
protected AbstractQueue() :默认构造函数,但是是抽象的,它不允许创建 AbstractQueue 对象。该实现应由其子类之一提供,例如 ArrayBlockingQueue、ConcurrentLinkedQueue、DelayQueue、LinkedBlockingDeque、LinkedBlockingQueue、LinkedTransferQueue、PriorityBlockingQueue、PriorityQueue、 SynchronousQueue 。
AbstractQueue objName = new ArrayBlockingQueue();
下面是一个示例程序来说明Java中的 AbstractQueue:
Java
// Java code to illustrate AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class AbstractQueueExample {
public static void main(String[] argv) throws Exception
{
// Creating object of AbstractQueue
AbstractQueue AQ = new LinkedBlockingQueue();
// Adding elements to the Queue
AQ.add(10);
AQ.add(20);
AQ.add(30);
AQ.add(40);
AQ.add(50);
// print the queue contents to the console
System.out.println("AbstractQueue contains: " + AQ);
}
}
Java
// Java program to illustrate the
// adding elements to the AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class AddingElementsExample {
public static void main(String[] argv) throws Exception
{
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ1 = new LinkedBlockingQueue();
// Populating AQ
AQ1.add(10);
AQ1.add(20);
AQ1.add(30);
AQ1.add(40);
AQ1.add(50);
// print AQ
System.out.println("AbstractQueue contains : "
+ AQ1);
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ2 = new LinkedBlockingQueue();
// print AQ2 initially
System.out.println("AbstractQueue2 initially contains : " + AQ2);
// adds elements of AQ1 in AQ2
AQ2.addAll(AQ1);
System.out.println( "AbstractQueue1 after addition contains : " + AQ2);
}
}
Java
// Java program to illustrate the
// removal of elements from AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class RemovingElementsExample {
public static void main(String[] argv) throws Exception
{
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ1 = new LinkedBlockingQueue();
// Add elements using add method
AQ1.add(10);
AQ1.add(20);
AQ1.add(30);
AQ1.add(40);
AQ1.add(50);
// print the queue contents to the console
System.out.println("AbstractQueue1 contains : " + AQ1);
// Retrieves the head
int head = AQ1.remove();
// print the head element to the console
System.out.println("head : " + head);
// print the modified queue
System.out.println("AbstractQueue1 after removal of head : " + AQ1);
// remove all the elements
AQ1.clear();
// print the modified queue
System.out.println("AbstractQueue1 : " + AQ1);
}
}
Java
// Java program to illustrate the
// accessing element from AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class AccessingElementExample {
public static void main(String[] argv) throws Exception
{
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ1 = new LinkedBlockingQueue();
// Populating AQ1 using add method
AQ1.add(10);
AQ1.add(20);
AQ1.add(30);
AQ1.add(40);
AQ1.add(50);
// print AQ to the console
System.out.println("AbstractQueue1 contains : " + AQ1);
// access the head element
System.out.println("head : " + AQ1.element());
}
}
AbstractQueue contains: [10, 20, 30, 40, 50]
基本操作
1.添加元素
要将元素添加到 AbstractQueue 中,它提供了两种方法。如果可以在不违反容量限制的情况下立即插入,则 add(E e) 方法将指定元素插入此队列。它在成功时返回 true,如果当前没有可用空间,则抛出IllegalStateException 。 addAll(E e) 方法将指定集合中的所有元素添加到此队列中。
Java
// Java program to illustrate the
// adding elements to the AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class AddingElementsExample {
public static void main(String[] argv) throws Exception
{
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ1 = new LinkedBlockingQueue();
// Populating AQ
AQ1.add(10);
AQ1.add(20);
AQ1.add(30);
AQ1.add(40);
AQ1.add(50);
// print AQ
System.out.println("AbstractQueue contains : "
+ AQ1);
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ2 = new LinkedBlockingQueue();
// print AQ2 initially
System.out.println("AbstractQueue2 initially contains : " + AQ2);
// adds elements of AQ1 in AQ2
AQ2.addAll(AQ1);
System.out.println( "AbstractQueue1 after addition contains : " + AQ2);
}
}
AbstractQueue contains : [10, 20, 30, 40, 50]
AbstractQueue2 initially contains : []
AbstractQueue1 after addition contains : [10, 20, 30, 40, 50]
2.删除元素
为了从 AbstractQueue 中移除元素,它提供了 remove() 和 clear() 方法。
- remove() 方法返回并移除此队列的头部。
- clear() 方法从这个队列中移除所有元素。此调用返回后,队列将为空。
Java
// Java program to illustrate the
// removal of elements from AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class RemovingElementsExample {
public static void main(String[] argv) throws Exception
{
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ1 = new LinkedBlockingQueue();
// Add elements using add method
AQ1.add(10);
AQ1.add(20);
AQ1.add(30);
AQ1.add(40);
AQ1.add(50);
// print the queue contents to the console
System.out.println("AbstractQueue1 contains : " + AQ1);
// Retrieves the head
int head = AQ1.remove();
// print the head element to the console
System.out.println("head : " + head);
// print the modified queue
System.out.println("AbstractQueue1 after removal of head : " + AQ1);
// remove all the elements
AQ1.clear();
// print the modified queue
System.out.println("AbstractQueue1 : " + AQ1);
}
}
AbstractQueue1 contains : [10, 20, 30, 40, 50]
head : 10
AbstractQueue1 after removal of head : [20, 30, 40, 50]
AbstractQueue1 : []
3. 访问元素
AbstractQueue 的 element() 方法检索但不删除此队列的头部。
Java
// Java program to illustrate the
// accessing element from AbstractQueue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class AccessingElementExample {
public static void main(String[] argv) throws Exception
{
// Since AbstractQueue is an abstract class
// create object using LinkedBlockingQueue
AbstractQueue AQ1 = new LinkedBlockingQueue();
// Populating AQ1 using add method
AQ1.add(10);
AQ1.add(20);
AQ1.add(30);
AQ1.add(40);
AQ1.add(50);
// print AQ to the console
System.out.println("AbstractQueue1 contains : " + AQ1);
// access the head element
System.out.println("head : " + AQ1.element());
}
}
AbstractQueue1 contains : [10, 20, 30, 40, 50]
head : 10
AbstractQueue 的方法
METHOD | DESCRIPTION |
---|---|
add(E e) | Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success, and throwing an IllegalStateException if no space is currently available. |
addAll(Collection extends E> c) | Adds all the elements in the specified collection to this queue. |
clear() | Removes all the elements from this queue. |
element() | Retrieves, but does not remove, the head of this queue. |
remove() | Retrieves and removes the head of this queue. |
在类Java.util.AbstractCollection 中声明的方法
METHOD | DESCRIPTION |
---|---|
contains(Object o) | Returns true if this collection contains the specified element. |
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
isEmpty() | Returns true if this collection contains no elements. |
iterator() | Returns an iterator over the elements contained in this collection. |
remove(Object o) | Removes a single instance of the specified element from this collection, if it is present (optional operation). |
removeAll(Collection> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
retainAll(Collection> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
toArray() | Returns an array containing all of the elements in this collection. |
toArray(T[] a) | Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
toString() | Returns a string representation of this collection. |
在接口Java.util.Collection 中声明的方法
METHOD | DESCRIPTION |
---|---|
contains(Object o) | Returns true if this collection contains the specified element. |
containsAll(Collection> c) | Returns true if this collection contains all the elements in the specified collection. |
equals(Object o) | Compares the specified object with this collection for equality. |
hashCode() | Returns the hash code value for this collection. |
isEmpty() | Returns true if this collection contains no elements. |
iterator() | Returns an iterator over the elements in this collection. |
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
remove(Object o) | Removes a single instance of the specified element from this collection, if it is present (optional operation). |
removeAll(Collection> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
removeIf(Predicate super E> filter) | Removes all the elements of this collection that satisfy the given predicate. |
retainAll(Collection> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
size() | Returns the number of elements in this collection. |
spliterator() | Creates a Spliterator over the elements in this collection. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray() | Returns an array containing all the elements in this collection. |
toArray(IntFunction | Returns an array containing all the elements in this collection, using the provided generator function to allocate the returned array. |
toArray(T[] a) | Returns an array containing all the elements in this collection; the runtime type of the returned array is that of the specified array. |
在接口Java.lang.Iterable 中声明的方法
METHOD | DESCRIPTION |
---|---|
forEach(Consumer super T> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
在接口Java .util.Queue 中声明的方法
METHOD | DESCRIPTION |
---|---|
offer(E e) | Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. |
peek() | Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
poll() | Retrieves and removes the head of this queue, or returns null if this queue is empty. |
参考: Java : Java