Queue<E> 컬렉션 인터페이스

jaegeunsong97·2023년 1월 21일
0
post-thumbnail

2023_1_21_TIL

Queue< E > 컬렉션의 특징

  • 스스로 객체를 못만든다 -> LinkedList< E >가 Queue< E >인터페이스의 구현 클래스
  • 상속구조
    • LinkedList< E > => Queue< E > => Collection< E >
  • FIFO

Queue< E >의 주요 메소드

  • 데이터가 없을 때 예외 발생
    • add(), element(), remove()
  • 데이터가 없을 때 기본값으로 대체 -> 훨씬 안전(사용 권장)
    • offer(), peek(), poll()
public class QueueMethod {
    public static void main(String[] args) {
        Queue<Integer> queue1 = new LinkedList<>();
        queue1.add(3);
        queue1.add(4);
        queue1.add(5);

        System.out.println(queue1.element());

        System.out.println(queue1.remove());
        System.out.println(queue1.remove());
        System.out.println(queue1.remove());
        // System.out.println(queue1.remove()); NoSuchElementException
        System.out.println();

        //
        Queue<Integer> queue2 = new LinkedList<>();
        queue2.offer(3);
        queue2.offer(4);
        queue2.offer(5);

        System.out.println(queue2.peek());

        System.out.println(queue2.poll());
        System.out.println(queue2.poll());
        System.out.println(queue2.poll());
        System.out.println(queue2.poll());
    }
}

참조

https://www.forbes.com/sites/greatspeculations/2021/12/06/fifo-vs-specific-identification-accounting-methods/?sh=75854e4e5080

profile
현재 블로그 : https://jasonsong97.tistory.com/

0개의 댓글