Home > Java > Lists

Java | Queues

Basics

// Create. Out: []
Queue<String> messageQueue = new LinkedList<>();

// Create + Init. Out: [600, -200]
Queue<Integer> transactionQueue = new LinkedList<>(List.of(600, -200));

// Add element. Out: [Hi., Call me., Ok, I'll call you in 5 min., Thanks.]
messageQueue.add("Hi.");
messageQueue.addAll(List.of("Call me.", "Ok, I'll call you in 5 min."));
messageQueue.offer("Thanks.");

// Get Size. Out: Message queue - 4, Transaction queue - 2
messageQueue.size();
transactionQueue.size();

// Retrieve, but not remove, the head. Out: 600, Hi.
transactionQueue.element();
messageQueue.peek();

// Retrieve and remove head. Out: 600, Hi.
transactionQueue.remove();
messageQueue.poll();

// Remove node by value. Out: true, [Ok, I'll call you in 5 min., Thanks.]
messageQueue.remove("Call me.");

// Clear. Out: []
messageQueue.clear();

Iterate

// loop with poll()
while (!queue.isEmpty()) {
    String element = queue.poll();
    System.out.println(element);
}

// for-each loop
for (String element : queue) {
    System.out.println(element);
}

// Temporary queue
Queue<String> tempQueue = new LinkedList<>(queue);
while (!tempQueue.isEmpty()) {
    String element = tempQueue.poll();
    System.out.println(element);
}

// Using streams.
Stream<String> stream = queue.stream();
stream.forEach(element -> System.out.println(element));

References