Java : AbstractQueue 示例
Java 中的 AbstractQueue (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 AbstractQueue<E> 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
public class MyQueue extends AbstractQueue<String> {
private final List<String> values = new ArrayList<>();
@Override
public Iterator<String> iterator() {
return values.iterator();
}
@Override
public int size() {
return values.size();
}
@Override
public boolean offer(String s) {
Objects.requireNonNull(s);
return values.add(s);
}
@Override
public String poll() {
return values.isEmpty() ? null : values.removeFirst();
}
@Override
public String peek() {
return values.isEmpty() ? null : values.getFirst();
}
}
Note : The MyQueue class uses ArrayList for implementation, but it may not be efficient.
final var queue = new MyQueue();
queue.offer("aaa");
queue.offer("bbb");
queue.offer("ccc");
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.poll()); // aaa
System.out.println(queue.poll()); // bbb
System.out.println(queue.poll()); // ccc
Constructors
AbstractQueue ()
public class MyQueue extends AbstractQueue<String> {
private final List<String> values = new ArrayList<>();
@Override
public Iterator<String> iterator() {
return values.iterator();
}
@Override
public int size() {
return values.size();
}
@Override
public boolean offer(String s) {
Objects.requireNonNull(s);
return values.add(s);
}
@Override
public String poll() {
return values.isEmpty() ? null : values.removeFirst();
}
@Override
public String peek() {
return values.isEmpty() ? null : values.getFirst();
}
}
Note : The MyQueue class uses ArrayList for implementation, but it may not be efficient.
final var queue = new MyQueue();
queue.offer("aaa");
queue.offer("bbb");
queue.offer("ccc");
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.poll()); // aaa
System.out.println(queue.poll()); // bbb
System.out.println(queue.poll()); // ccc
Methods
boolean add (E e)
// capacity = 3
final AbstractQueue<String> queue = new ArrayBlockingQueue<>(3);
System.out.println(queue); // []
System.out.println(queue.add("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.add("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.add("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
try {
queue.add("ddd");
} catch (IllegalStateException e) {
System.out.println("IllegalStateException! : " + e.getMessage());
}
// Result
// ↓
//IllegalStateException! : Queue full
boolean addAll (Collection<? extends E> c)
final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []
System.out.println(queue.addAll(List.of("a1"))); // true
System.out.println(queue); // [a1]
System.out.println(queue.addAll(List.of("b1", "b2"))); // true
System.out.println(queue); // [a1, b1, b2]
System.out.println(queue.addAll(List.of("c1", "c2", "c3"))); // true
System.out.println(queue); // [a1, b1, b2, c1, c2, c3]
void clear ()
final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []
queue.add("a");
System.out.println(queue); // [a]
queue.add("b");
System.out.println(queue); // [a, b]
queue.clear();
System.out.println(queue); // []
E element ()
final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
System.out.println(queue.element()); // aaa
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.remove()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.element()); // bbb
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.remove()); // bbb
System.out.println(queue); // [ccc]
System.out.println(queue.element()); // ccc
System.out.println(queue); // [ccc]
System.out.println(queue.remove()); // ccc
System.out.println(queue); // []
try {
queue.element();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
E remove ()
final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.remove()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.remove()); // bbb
System.out.println(queue); // [ccc]
System.out.println(queue.remove()); // ccc
System.out.println(queue); // []
try {
queue.remove();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
Methods declared in AbstractCollection
contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, toArray, toArray, toString
请参阅下面的链接。
Methods declared in Collection
contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
请参阅下面的链接。