[Java] Queue, Deque

When I was studying Java Gold, it came out, so I scribbled it.

Queue<E> Inherit Collection \ . That is, it is a thing for using multiple data together like an array. Then, when I looked up the word Queue on a certain English translation site to see what makes it different from other collections,

** 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.

Method

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 \ . The official name is "Double Ended Queue" As the name implies, it is a queue that allows you to insert and delete elements from both ends.

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.

Method

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

[Java] Queue, Deque
Java
Java
[Java] Collection-List / Set / Map / Stack / Queue
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
java notes
java beginner
[java] interface
Java9 collection
Java basics
Java methods
Java diary
Java inheritance
[Java] enum
[Java] FizzBuzzCounter
Studying Java ―― 8