Java中的 ConcurrentLinkedDeque 示例
Java中的ConcurrentLinkedDeque类是Java集合框架的一部分,实现了Collection 接口和AbstractCollection 类。它属于Java.util.concurrent包。它用于在 LinkedList 的帮助下同时实现 Deque。
ConcurrentLinkedDeque 的特点
- 迭代器和拆分器是弱一致的。
- 并发插入、删除和访问操作跨多个线程安全执行。
- 它不允许空元素。
- size() 方法不是在恒定时间内实现的。由于这些双端队列的异步特性,确定当前元素的数量需要遍历元素。
类层次结构:
宣言:
public abstract class ConcurrentLinkedDeque
extends AbstractCollection
implements Deque, Serializable
Here, E is the type of elements maintained by this collection.
它实现了 Serializable、Iterable
ConcurrentLinkedDeque 的构造函数:
1. ConcurrentLinkedDeque() :该构造函数用于构造一个空的双端队列。
ConcurrentLinkedDeque
2. ConcurrentLinkedDeque(Collection
ConcurrentLinkedDeque
下面是说明Java中的 ConcurrentLinkedDeque 的示例程序:
Java
// Java Program to demonstrate ConcurrentLinkedDeque
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
// Create a ConcurrentLinkedDeque
// using ConcurrentLinkedDeque()
// constructor
ConcurrentLinkedDeque
cld = new ConcurrentLinkedDeque();
// add element to the front
// using addFirst() method
cld.addFirst(12);
cld.addFirst(70);
cld.addFirst(1009);
cld.addFirst(475);
// Displaying the existing ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
// Create a ConcurrentLinkedDeque
// using ConcurrentLinkedDeque(Collection c)
// constructor
ConcurrentLinkedDeque
cld1 = new ConcurrentLinkedDeque(cld);
// Displaying the existing ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque1: "
+ cld1);
}
}
Java
// Java code to illustrate
// methods of ConcurrentLinkedDeque
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
// Create a ConcurrentLinkedDeque
// using ConcurrentLinkedDeque() constructor
ConcurrentLinkedDeque
cld = new ConcurrentLinkedDeque();
// add element to the front
// using addFirst() method
cld.addFirst(12);
cld.addFirst(70);
cld.addFirst(1009);
cld.addFirst(475);
// Displaying the existing ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
// Displaying the Last element
// using getLast() method
System.out.println("The Last element is: "
+ cld.getLast());
// Displaying the first element
// using peekFirst() method
System.out.println("First Element is: "
+ cld.peekFirst());
// Remove the Last element
// using removeLast() method
cld.removeLast();
// Displaying the existing ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
}
}
Java
// Java Program Demonstrate adding
// elements to the ConcurrentLinkedDeque
import java.util.concurrent.*;
class AddingElements {
public static void main(String[] args)
{
// create instance using ConcurrentLinkedDeque
ConcurrentLinkedDeque cld1
= new ConcurrentLinkedDeque();
// Add element to the tail using
// add or addLast methods
cld1.add(12);
cld1.add(110);
// Add element to the head
// using addFirst method
cld1.addFirst(55);
// Displaying the existing ConcurrentLinkedDeque
System.out.println("Initial Elements in "
+ "the LinkedDeque cld : "
+ cld1);
// create instance using ConcurrentLinkedDeque
ConcurrentLinkedDeque cld2
= new ConcurrentLinkedDeque();
// Add elements of cld1 to the
// cld2 using addAll method
cld2.addAll(cld1);
// Displaying the modified ConcurrentLinkedDeque
System.out.println("Initial Elements in "
+ "the LinkedDeque cld2: "
+ cld2);
}
}
Java
// Java Program to demonstrate removing
// elements of ConcurrentLinkedDeque
import java.util.concurrent.*;
class RemovingElements {
public static void main(String[] args)
{
// Create a ConcurrentLinkedDeque
// using ConcurrentLinkedDeque() constructor
ConcurrentLinkedDeque cld
= new ConcurrentLinkedDeque();
// Add elements using add() method
cld.add(40);
cld.add(50);
cld.add(60);
cld.add(70);
cld.add(80);
// Displaying the existing LinkedDeque
System.out.println(
"Existing ConcurrentLinkedDeque: " + cld);
// remove method removes the first
// element of ConcurrentLinkedDeque
// using remove() method
System.out.println("Element removed: "
+ cld.remove());
// Remove 60 using remove(Object)
System.out.println("60 removed: " + cld.remove(60));
// Displaying the existing ConcurrentLinkedDeque
System.out.println(
"Modified ConcurrentLinkedDeque: " + cld);
// Remove the first element
cld.removeFirst();
// Remove the Last element
cld.removeLast();
// Displaying the existing ConcurrentLinkedDeque
System.out.println(
"Modified ConcurrentLinkedDeque: " + cld);
}
}
Java
// Java code to illustrate iterating
// elements of ConcurrentLinkedDeque
import java.util.concurrent.*;
import java.util.*;
public class IteratingConcurrentLinkedDeque {
public static void main(String args[])
{
// Creating an empty ConcurrentLinkedDeque
ConcurrentLinkedDeque deque
= new ConcurrentLinkedDeque();
// Use add() method to add elements
// into the ConcurrentLinkedDeque
deque.add("Welcome");
deque.add("To");
deque.add("Geeks");
deque.add("4");
deque.add("Geeks");
// Displaying the ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ deque);
// Creating an iterator
Iterator fitr = deque.iterator();
// Displaying the values
// after iterating through the ConcurrentLinkedDeque
System.out.println("The iterator values are: ");
while (fitr.hasNext()) {
System.out.println(fitr.next());
}
// Creating a desc_iterator
Iterator ditr = deque.descendingIterator();
// Displaying the values after iterating
// through the ConcurrentLinkedDeque
// in reverse order
System.out.println("The iterator values are: ");
while (ditr.hasNext()) {
System.out.println(ditr.next());
}
}
}
Java
// Java Program to Demonstrate accessing
// elements of ConcurrentLinkedDeque
import java.util.concurrent.*;
import java.util.*;
class Accessing {
public static void main(String[] args)
{
// Creating an empty ConcurrentLinkedDeque
ConcurrentLinkedDeque cld
= new ConcurrentLinkedDeque();
// Add elements into the ConcurrentLinkedDeque
cld.add("Welcome");
cld.add("To");
cld.add("Geeks");
cld.add("4");
cld.add("Geeks");
// Displaying the ConcurrentLinkedDeque
System.out.println("Elements in the ConcurrentLinkedDeque: " + cld);
// Displaying the first element
System.out.println("The first element is: "
+ cld.getFirst());
// Displaying the Last element
System.out.println("The Last element is: "
+ cld.getLast());
// Displaying the head of ConcurrentLinkedDeque
System.out.println("The Head of ConcurrentLinkedDeque is: "
+ cld.element());
}
}
ConcurrentLinkedDeque: [475, 1009, 70, 12]
ConcurrentLinkedDeque1: [475, 1009, 70, 12]
例子:
Java
// Java code to illustrate
// methods of ConcurrentLinkedDeque
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
// Create a ConcurrentLinkedDeque
// using ConcurrentLinkedDeque() constructor
ConcurrentLinkedDeque
cld = new ConcurrentLinkedDeque();
// add element to the front
// using addFirst() method
cld.addFirst(12);
cld.addFirst(70);
cld.addFirst(1009);
cld.addFirst(475);
// Displaying the existing ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
// Displaying the Last element
// using getLast() method
System.out.println("The Last element is: "
+ cld.getLast());
// Displaying the first element
// using peekFirst() method
System.out.println("First Element is: "
+ cld.peekFirst());
// Remove the Last element
// using removeLast() method
cld.removeLast();
// Displaying the existing ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
}
}
ConcurrentLinkedDeque: [475, 1009, 70, 12]
The Last element is: 12
First Element is: 475
ConcurrentLinkedDeque: [475, 1009, 70]
ConcurrentLinkedDeque 的基本操作
1.添加元素
为了添加一个元素或元素集合,ConcurrentLinkedDeque 提供了 add(E e)、addAll(Collection extends E> c)、addFirst(E e)、addLast(E e) 等方法。下面的示例解释了这些方法。
Java
// Java Program Demonstrate adding
// elements to the ConcurrentLinkedDeque
import java.util.concurrent.*;
class AddingElements {
public static void main(String[] args)
{
// create instance using ConcurrentLinkedDeque
ConcurrentLinkedDeque cld1
= new ConcurrentLinkedDeque();
// Add element to the tail using
// add or addLast methods
cld1.add(12);
cld1.add(110);
// Add element to the head
// using addFirst method
cld1.addFirst(55);
// Displaying the existing ConcurrentLinkedDeque
System.out.println("Initial Elements in "
+ "the LinkedDeque cld : "
+ cld1);
// create instance using ConcurrentLinkedDeque
ConcurrentLinkedDeque cld2
= new ConcurrentLinkedDeque();
// Add elements of cld1 to the
// cld2 using addAll method
cld2.addAll(cld1);
// Displaying the modified ConcurrentLinkedDeque
System.out.println("Initial Elements in "
+ "the LinkedDeque cld2: "
+ cld2);
}
}
输出:
Initial Elements in the LinkedDeque cld : [55, 12, 110]
Initial Elements in the LinkedDeque cld2: [55, 12, 110]
2.删除元素
为了移除一个元素,ConcurrentLinkedDeque 提供了 remove()、remove(Object o)、removeFirst()、removeLast() 等方法。这些方法在下面的示例中进行了说明。
Java
// Java Program to demonstrate removing
// elements of ConcurrentLinkedDeque
import java.util.concurrent.*;
class RemovingElements {
public static void main(String[] args)
{
// Create a ConcurrentLinkedDeque
// using ConcurrentLinkedDeque() constructor
ConcurrentLinkedDeque cld
= new ConcurrentLinkedDeque();
// Add elements using add() method
cld.add(40);
cld.add(50);
cld.add(60);
cld.add(70);
cld.add(80);
// Displaying the existing LinkedDeque
System.out.println(
"Existing ConcurrentLinkedDeque: " + cld);
// remove method removes the first
// element of ConcurrentLinkedDeque
// using remove() method
System.out.println("Element removed: "
+ cld.remove());
// Remove 60 using remove(Object)
System.out.println("60 removed: " + cld.remove(60));
// Displaying the existing ConcurrentLinkedDeque
System.out.println(
"Modified ConcurrentLinkedDeque: " + cld);
// Remove the first element
cld.removeFirst();
// Remove the Last element
cld.removeLast();
// Displaying the existing ConcurrentLinkedDeque
System.out.println(
"Modified ConcurrentLinkedDeque: " + cld);
}
}
Existing ConcurrentLinkedDeque: [40, 50, 60, 70, 80]
Element removed: 40
60 removed: true
Modified ConcurrentLinkedDeque: [50, 70, 80]
Modified ConcurrentLinkedDeque: [70]
3. 迭代元素
我们可以使用 iterator() 或 descendingIterator() 方法迭代 ConcurrentLinkedDeque。下面的代码解释了这两种方法。
Java
// Java code to illustrate iterating
// elements of ConcurrentLinkedDeque
import java.util.concurrent.*;
import java.util.*;
public class IteratingConcurrentLinkedDeque {
public static void main(String args[])
{
// Creating an empty ConcurrentLinkedDeque
ConcurrentLinkedDeque deque
= new ConcurrentLinkedDeque();
// Use add() method to add elements
// into the ConcurrentLinkedDeque
deque.add("Welcome");
deque.add("To");
deque.add("Geeks");
deque.add("4");
deque.add("Geeks");
// Displaying the ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque: "
+ deque);
// Creating an iterator
Iterator fitr = deque.iterator();
// Displaying the values
// after iterating through the ConcurrentLinkedDeque
System.out.println("The iterator values are: ");
while (fitr.hasNext()) {
System.out.println(fitr.next());
}
// Creating a desc_iterator
Iterator ditr = deque.descendingIterator();
// Displaying the values after iterating
// through the ConcurrentLinkedDeque
// in reverse order
System.out.println("The iterator values are: ");
while (ditr.hasNext()) {
System.out.println(ditr.next());
}
}
}
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
The iterator values are:
Welcome
To
Geeks
4
Geeks
The iterator values are:
Geeks
4
Geeks
To
Welcome
4. 访问元素
为了访问 ConcurrentLinkedDeque 的元素,它提供了 getFirst()、getLast()、element() 等方法。下面的示例解释了这些方法。
Java
// Java Program to Demonstrate accessing
// elements of ConcurrentLinkedDeque
import java.util.concurrent.*;
import java.util.*;
class Accessing {
public static void main(String[] args)
{
// Creating an empty ConcurrentLinkedDeque
ConcurrentLinkedDeque cld
= new ConcurrentLinkedDeque();
// Add elements into the ConcurrentLinkedDeque
cld.add("Welcome");
cld.add("To");
cld.add("Geeks");
cld.add("4");
cld.add("Geeks");
// Displaying the ConcurrentLinkedDeque
System.out.println("Elements in the ConcurrentLinkedDeque: " + cld);
// Displaying the first element
System.out.println("The first element is: "
+ cld.getFirst());
// Displaying the Last element
System.out.println("The Last element is: "
+ cld.getLast());
// Displaying the head of ConcurrentLinkedDeque
System.out.println("The Head of ConcurrentLinkedDeque is: "
+ cld.element());
}
}
输出:
Elements in the ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
The first element is: Welcome
The Last element is: Geeks
The Head of ConcurrentLinkedDeque is: Welcome
ConcurrentLinkedDeque 的方法
这里, E是 元素的类型。 METHOD DESCRIPTIONadd(E e) Inserts the specified element at the tail of this deque. addAll(Collection extends E> c) Appends all of the elements in the specified collection to the end of this deque, in the order that they are returned by the specified collection’s iterator. addFirst(E e) Inserts the specified element at the front of this deque. addLast(E e) Inserts the specified element at the end of this deque. clear() Removes all of the elements from this deque. contains(Object o) Returns true if this deque contains the specified element. descendingIterator() Returns an iterator over the elements in this deque in reverse sequential order. element() Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). forEach(Consumer super E> action) Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. getFirst() Retrieves, but does not remove, the first element of this deque. getLast() Retrieves, but does not remove, the last element of this deque. isEmpty() Returns true if this collection contains no elements. iterator() Returns an iterator over the elements in this deque in the proper sequence. offer(E e) Inserts the specified element at the tail of this deque. offerFirst(E e) Inserts the specified element at the front of this deque. offerLast(E e) Inserts the specified element at the end of this deque. pop() Pops an element from the stack represented by this deque. push(E e) Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. remove() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). remove(Object o) Removes the first occurrence of the specified element from this deque. removeAll(Collection> c) Removes all of this collection’s elements that are also contained in the specified collection (optional operation). removeFirst() Retrieves and removes the first element of this deque. removeFirstOccurrence(Object o) Removes the first occurrence of the specified element from this deque. removeIf(Predicate super E> filter) Removes all of the elements of this collection that satisfy the given predicate. removeLast() Retrieves and removes the last element of this deque. removeLastOccurrence(Object o) Removes the last occurrence of the specified element from this deque. 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 deque. spliterator() Returns a Spliterator over the elements in this deque. toArray() Returns an array containing all of the elements in this deque, in proper sequence (from first to last element). toArray(T[] a) Returns an array containing all of the elements in this deque, in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
在类Java.util.AbstractCollection 中声明的方法
METHOD | DESCRIPTION |
---|---|
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
toString() | Returns a string representation of this collection. |
在接口Java.util.Collection 中声明的方法
METHOD | DESCRIPTION |
---|---|
containsAll(Collection> c) | Returns true if this collection contains all of 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. |
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray(IntFunction | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
在接口Java .util.Deque 中声明的方法
METHOD | DESCRIPTION |
---|---|
peek() | Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty. |
peekFirst() | Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. |
peekLast() | Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. |
poll() | Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty. |
pollFirst() | Retrieves and removes the first element of this deque, or returns null if this deque is empty. |
pollLast() | Retrieves and removes the last element of this deque, or returns null if this deque is empty. |
参考: Java : Java