import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target= "_blank">Primitive PriorityQueue<E></a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {

	/**
	 * @see Queue#forEach(java.util.function.Consumer)
	 *
	 */
	public static void applayLoop(Queue<Integer> buffer) {
		buffer.forEach(i -> System.out.print("e=" + i + " "));
		println();
	}

	/**
	 * @see Queue#peek()
	 *
	 */
	public static void applayMethodPeek(Queue<Integer> buffer) {
		applayLoop(buffer);
		System.out.println(buffer.peek());
		applayLoop(buffer);
	}

	/**
	 * @see Queue#poll()
	 *
	 */
	public static void applayMethodPoll(Queue<Integer> buffer) {
		applayLoop(buffer);
		System.out.println(buffer.poll());
		applayLoop(buffer);
	}

	/**
	 * @see Queue#iterator()
	 * @see Iterator#hasNext()
	 *
	 */
	public static void applayMethodIterator(Queue<Integer> buffer) {
		for (Iterator<Integer> itr = buffer.iterator(); itr.hasNext();)
			System.out.print("e=" + itr.next() + " ");

		println();
	}

	private static void println() {
		System.out.println();
	}

	/**
	 * @see Queue#offer(Object)
	 *
	 */
	private static PriorityQueue<Integer> createBufferTest() {
		return new PriorityQueue<Integer>() {
			private static final long serialVersionUID = -69L;

			{
				// inserzione con priorità NATURALE
				offer((Integer) 6); // [[6]] <- 5
				offer((Integer) 5); // [[5, 6]] <- 3
				offer((Integer) 3); // [[3, 6], 5] <- 8
				offer((Integer) 8); // [[3, 6], [5, 8]] <- 0
				offer((Integer) 0); // [[0, 3], [5, 8, 6]] <- 1
				offer((Integer) 1); // [[0, 3, 1] 8, 6, 5] <- 7
				offer((Integer) 7); // [[0, 3, 1] 8, [6, 5], 7]
			}
		};
	}

	public static void main(String args[]) throws Exception {
		// Unit test - attraversa una struttura
		applayLoop(createBufferTest());

		// Unit test - attraversa una struttura con l'ausilio di un'interfaccia
		// Iteretor<E>
		applayMethodIterator(createBufferTest());

		// Unit test - estrazione dell'elemento in testa alla struttura ( SENZA
		// RIMOZIONE )
		applayMethodPeek(createBufferTest());

		// Unit test - estrazione dell'elemento in testa alla struttura ( CON RIMOZIONE
		// )
		applayMethodPoll(createBufferTest());
	}
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: