Buscar..


Introducción

Un Deque es una colección lineal que admite la inserción y eliminación de elementos en ambos extremos.

El nombre deque es corto para "doble cola de espera" y generalmente se pronuncia "deck".

La mayoría de las implementaciones de Deque no establecen límites fijos en el número de elementos que pueden contener, pero esta interfaz es compatible con los deques con capacidad limitada, así como aquellos sin límite de tamaño fijo.

La interfaz de Deque es un tipo de datos abstracto más rico que tanto la Pila como la Cola porque implementa pilas y colas al mismo tiempo

Observaciones

Los genéricos se pueden utilizar con Deque.

Deque<Object> deque = new LinkedList<Object>();

Cuando se utiliza un deque como una cola, se produce el comportamiento FIFO (primero en entrar, primero en salir).

Deques también se puede utilizar como pilas LIFO (Last-In-First-Out-Out).

Para obtener más información sobre los métodos, consulte esta documentación.

Añadiendo elementos a Deque

Deque deque = new LinkedList();

//Adding element at tail
deque.add("Item1");

//Adding element at head 
deque.addFirst("Item2");

//Adding element at tail 
deque.addLast("Item3");

Eliminando Elementos de Deque

//Retrieves and removes the head of the queue represented by this deque
Object headItem = deque.remove();

//Retrieves and removes the first element of this deque.
Object firstItem = deque.removeFirst();

//Retrieves and removes the last element of this deque.
Object lastItem = deque.removeLast();

Recuperando elemento sin quitar

//Retrieves, but does not remove, the head of the queue represented by this deque
Object headItem = deque.element();

//Retrieves, but does not remove, the first element of this deque.
Object firstItem = deque.getFirst();

//Retrieves, but does not remove, the last element of this deque.    
Object lastItem  = deque.getLast();

Iterando a través de Deque

//Using Iterator
Iterator iterator = deque.iterator();
while(iterator.hasNext(){
  String Item = (String) iterator.next();
}

//Using For Loop
for(Object object : deque) {
    String Item = (String) object;
}


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow