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

Java How to use Queue?

// LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();

// Array implementation of Queue
Queue<String> animal2 = new ArrayDeque<>();

// Priority Queue implementation of Queue
Queue<String> animal 3 = new PriorityQueue<>();
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 join two list 
Java :: convert arraylist to csv file java 
Java :: java insert char in string at index 
Java :: awk print second 
Java :: tableau de classe java 
Java :: set size button java 
Java :: explicit casting 
Java :: square root of a number in java without sqrt 
Java :: java float 0/0 
Java :: add opacity to activity android 
Java :: what does static mean java 
Java :: java secretkey 
Java :: Failed to collect dependencies at net.sf.jasperreports:jasperreports:jar:6.2.0 
Java :: Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/webview 
Java :: how to modify string in java 
Java :: for loop javasctip 
Java :: how to add element to end of array java 
Java :: retainall java 
Java :: creating a 2d arraylist in java 
Java :: for each loop java string array 
Java :: How to convert long to string android studio - java 
Java :: how to check base64 in java 
Java :: string to int 
Java :: android studio download 
Java :: convert fileinputstream to string java 
Java :: java wait(timeout) 
Java :: camera permission in android 
Java :: generic list java 
Java :: how to find palindrome numbers in java 
Java :: crear objetos automaticamente java 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =