Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use deque as queue in java?

/*
	This implementation shows how to use a Deque
	object as a stack and then how to use it
	as a queue.

	Time complexity of methods used: O(1)
*/
import java.util.Deque;
import java.util.ArrayDeque;
public class DequeDemo {
	public static void main(String[] args) {
		Deque<Integer> deque = new ArrayDeque<>();
		// Deque as stack
		deque.push(4); // Insert a new top element
		deque.push(3);
		// Pop top element from stack
		System.out.println(deque.getFirst()); // 3
		deque.pop();
		System.out.println(deque.pop()); // 4

		// Deque as Queue
		deque.add(5); // Enqueue an element
		deque.add(2);
		System.out.println(deque.getFirst()); // 5
		// Dequeue the front element of queue
   		deque.removeFirst();
		System.out.println(deque.removeFirst()); // 2
	}
}
Comment

dequeue element to queue java

Queue<String> queue = new LinkedList<>();

queue.add("element 1");
queue.add("element 2");

String element1 = queue.poll();

String element2 = queue.remove();
Comment

PREVIOUS NEXT
Code Example
Java :: java union of sets 
Java :: trivers json node as node type 
Java :: Read array of characters from space separated values in java 
Java :: string tmp java 
Java :: android volley benefits 
Java :: online currency rate api 
Java :: How to center a print statement text? 
Java :: add pd4j to application 
Java :: adding Executable Jars to Spring Maven Project 
Java :: verificar numero par ou impar jacva 
Java :: all GlideException#logRootCauses(String) for more detail Cause (1 of 1): class javax.net.ssl.SSLPeerUnverifiedException: Hostname 
Java :: Java headSet(element, booleanValue) 
Java :: get bimap by uri in android 
Java :: scanner class in java 
Java :: first method in jdbc 
Java :: kotlin dependency injection 
Java :: tests offline cypress 
Java :: search for a string in byte array 
Java :: recursive in java 
Java :: java inetaddress workaround how to get localhost secure 
Java :: hikari cp oracle jdbc configuration 
Java :: change replication factor hadoop cluster command 
Java :: ordenar mapa de forma descendente java 
Java :: java pattern matching 
Java :: How to handle exceptions thrown by application with another servlet? 
Java :: Access Members of a Class Java 
Java :: int array to map java 
Java :: setting up javafx in eclipse 
Java :: autowired in spring 
Java :: java join list 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =