정의
스택과 다르게 줄을 지어 순서대로 처리되는 자료구조이다.

가장 많이 사용되는 곳은
| 메서드 | 설명 |
|---|---|
| queue.add() | 데이터 추가 반환 값(boolean): 삽입 성공 시 true / 실패 시 Exception발생 |
| queue.offer() | 데이터 추가 반환 값(boolean): 삽입 성공 시 true / 실패 시 false 반환 |
| queue.poll() | 데이터 삭제 queue에 첫번째 값을 반환하고 제거 비어있다면 null |
| queue.remove() | 데이터 삭제 queue에 첫번째 값 제거 반환 값(삭제된 value의 자료형): 삭제된 value / 공백 큐이면 Exception("NoSuchElementException") 발생 |
| queue.clear() | 데이터 모든값 삭제 queue 초기화 반환값 void |
| queue.peek() | 데이터 첫번째 값 가져옴 queue의 첫번째 값 참조 반환값 큐 헤드에 위치한 value/공백이면 null |
| queue.size() | queue 사이즈 크기 반환값 int |
| queue.contains("찾고싶은 value") | 찾고 싶음 value값이 있는지 확인 있을경우 true/ 없으면 false 반환값 boolean |
| queue.isEmpty(); | 공백이면 true / 값이 있으면 false 반환값 boolean |
| queue.element(); | 큐의 첫번째 값 반환 공백큐이면 Exception("NoSuchElementException")발생 반환값 큐 헤드에 위치한 value |
| queue.remove(삭제할 value); | 반환 값(boolean): 큐에 해당 value가 존재하면 해당 값 삭제 후 true / 존재하지 않으면 false 반환 |
// Queue 선언하기
Queue queue = new LinkedList(); // 타입설정 X Object로 입력
Queue<Integer> q1 = new LinkedList<Integer>(); // Integer 타입으로 선언
Queue<Integer> q2 = new LinkedList<>(); // new 부분 타입생략 가능
Queue<Integer> q3 = new LinkedList<Integer>(Arrays.asList(1, 2, 3)); // 선언과 동시에 초기 값 세팅
Queue<String> str = new LinkedList<String>(); // String타입 선언
Queue<Character> ch = new LinkedList<Character>(); // Character타입 선언
str.offer("Hello");
str.offer("World");
System.out.println(str); // 값 출력 [Hello, World]
str.clear();
str.offer("Hello");
str.offer("World");
str.offer("Hello");
str.offer("Hello");
str.offer("World");
System.out.println(str); // 값 출력 [Hello, World, Hello, Hello, World]
str.poll(); // 삭제
System.out.println(str); // 값 출력 [World, Hello, Hello, World]
str.remove(); // 삭제
System.out.println(str); // 값 출력 [Hello, Hello, World]
str.remove("Hello"); // 해당하는 값 삭제
System.out.println(str); // 값 출력 [Hello, World]
str.clear(); // 모두 삭제
System.out.println(str); // 값 출력 []
str.offer("Hello");
str.offer("World");
str.offer("Hello");
str.offer("Hello");
str.offer("World");
System.out.println("Queue의 크기 : " + str.size()); // 값 출력 [Queue의 크기 : 5]
str.clear();
str.offer("Hello");
str.offer("World");
str.offer("Hello");
str.offer("Hello");
str.offer("World");
System.out.println("첫 번째 값 출력 : " + str.peek()); // 값 출력 [첫 번째 값 출력 : Hello]
/* Iterator 사용하여 모든 값 출력 */
for (String s : str) {
System.out.print(s + " "); // 값 출력 [Hello World Hello Hello World]
}
Queue<Integer> que = new LinkedList<Integer>(); // Integer변수 생성
int tmp = 0;
for (int i = 1; i <= 5; i++) {
que.offer(i); // 값 추가
}
System.out.println(que);
for (int i = 1; i <= 5; i++) {
tmp = que.peek(); // 첫 번째 값 return que.offer(tmp); // 값 추가.
System.out.println(que.remove()); // 첫 번째 값 삭제 및 출력
}
for (int i = 1 ; i <= 5 ; i++){
que.offer(i);
}
System.out.println(que);
que.offer(que.poll()); // 첫 번째 값을 삭제하며 동시에 값 추가 (= 앞의 값을 가장 뒤로)
System.out.println(que);
[1, 2, 3, 4, 5]
1
2
3
4
5
---------------------------------------
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 4, 5, 1]