Java : AbstractQueue con ejemplos
AbstractQueue (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos AbstractQueue<E>.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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
Consulte el siguiente enlace.
Methods declared in Collection
contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
Consulte el siguiente enlace.