queue java collection operations code example

Example 1: java queue

import java.util.*;

Queue<Integer> queue = new LinkedList<Integer>();
// use non-primative types when constructing

// to add a value to the back of queue:
queue.add(7);

// to remove and return front value:
int next = queue.remove();

// to just return front value without removing:
int peek = queue.peek();

Example 2: queue.poll() in java

//prints the head and deletes the head.
q=[10,20,30]
int n=q.poll();//returns 10   q=[20,30]
int m=q.poll();//returns 20   q=[30]

Tags:

Java Example