Java : Queue con ejemplos
Queue (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos Queue<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
final Queue<String> queue = new ArrayDeque<>();
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); // []
Methods
boolean add (E e)
// capacity = 3
final Queue<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
E element ()
final Queue<String> queue = new ArrayDeque<>();
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!
boolean offer (E e)
// capacity = 3
final Queue<String> queue = new ArrayBlockingQueue<>(3);
System.out.println(queue); // []
System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.offer("ddd")); // false
System.out.println(queue); // [aaa, bbb, ccc]
E peek ()
final Queue<String> queue = new ArrayDeque<>();
System.out.println(queue); // []
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
System.out.println(queue.peek()); // aaa
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.poll()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.peek()); // bbb
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.poll()); // bbb
System.out.println(queue); // [ccc]
System.out.println(queue.peek()); // ccc
System.out.println(queue); // [ccc]
System.out.println(queue.poll()); // ccc
System.out.println(queue); // []
System.out.println(queue.peek()); // null
System.out.println(queue); // []
E poll ()
final Queue<String> queue = new ArrayDeque<>();
System.out.println(queue); // []
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.poll()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.poll()); // bbb
System.out.println(queue); // [ccc]
System.out.println(queue.poll()); // ccc
System.out.println(queue); // []
System.out.println(queue.poll()); // null
System.out.println(queue); // []
E remove ()
final Queue<String> queue = new ArrayDeque<>();
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 Collection
addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
Consulte el siguiente enlace.
Methods declared in Iterable
Related posts
- Ejemplos de API