When I was studying Java Gold, it came out, so I scribbled it.
Queue<E>
Inherit Collection \
** Rows (people and vehicles waiting for their turn) **
And that. The inside of () is miso.
In other words, it has a data structure called "FIFO (First-in-First-out)" that allows you to retrieve the first thing you put in. People who are waiting for their turn will come first, and those who are in line will come first.
That's why it's sometimes called a "queue".
However, it does not mean that it has to be a separate FIFO implementation.
Throw an exception on error | Returns null on error | |
---|---|---|
Insert | add(e) | offer(e) |
Take out | remove() | poll() |
reference | element() | peak() |
QueueTest.java
import java.util.Queue;
import java.util.ArrayDeque;
public class QueueTest {
public void myQueue() {
Queue<Integer> queueTest = new ArrayDeque<Integer>();
for(int i=0; i < 10; i++) {
//Insert value into queue
queueTest.add(i);
}
//Insert value into queue
queueTest.offer(100);
System.out.println(queueTest);
//Extract the beginning
Integer r = queueTest.remove();
System.out.println("remove value: " + r);
//See top
System.out.println(queueTest.element());
//Extract the beginning
Integer p = queueTest.poll();
System.out.println("poll value: " + p);
//See top
System.out.println(queueTest.peek());
System.out.println(queueTest);
}
}
Execution result:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100]
remove value: 0
1
poll value: 1
2
[2, 3, 4, 5, 6, 7, 8, 9, 100]
Deque<E>
Inherit Queue \
By the way, Deque seems to be read as "deck". I thought, "Isn't it'dequeue'...", but with "dequeue" which means "operation to retrieve data" It was a mess. There must be someone who made the same mistake.
Throw an exception on error | Returns null on error | |
---|---|---|
Insert (top) | addFirst(e) | offerFirst(e) |
Insert (end) | removeFirst() | pollFirst() |
Take out (top) | getFirst() | peakFirst() |
Take out (end) | addLast() | offerLast() |
Reference (top) | removeLast() | pollLast() |
Reference (end) | getLast() | peakLast() |
Why isn't it the reference elementFirst / Last for the exception throw ...
DequeTest.java
import java.util.Deque;
import java.util.ArrayDeque;
public class DequeTest {
public void myDeque() {
Deque<Integer> dequeTest = new ArrayDeque<Integer>();
for(int i=0; i < 10; i++) {
//Insert value into queue from beginning
dequeTest.addFirst(i);
}
System.out.println(dequeTest);
//Extract the end
Integer r = dequeTest.removeLast();
System.out.println("remove value: " + r);
//See end
System.out.println(dequeTest.getLast());
//Extract the beginning
Integer p = dequeTest.pollFirst();
System.out.println("poll value: " + p);
//See top
System.out.println(dequeTest.peekFirst());
System.out.println(dequeTest);
}
}
Execution result:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
remove value: 0
1
poll value: 9
8
[8, 7, 6, 5, 4, 3, 2, 1]
You can see that the insertion order of the values from 0 to 9 is the reverse of Queue. Since it can be inserted from both directions, it can also be used as a stack that uses the "LIFO (Last-in-First-out) / last-in-first-out method".
Recommended Posts