본문 바로가기

알고리즘

Queue offer와 add 차이

Oracle 문서를 보면 Queue에 쓰이는 메소드는 예외처리를 하는 것과 특정값을 리턴해주는 두 부분을 나뉜다.

offer 메소드와 add 메소드는 요소를 삽입할 때 사용된다.

 

둘의 공통점은 둘다 요소를 삽입할 수 있을때 요소를 삽입한다는 점이다.

 

차이점은 queue가 가득차서 더이상 요소를 삽입할 수 없는 상황에서 offer은 삽입에 실패해도 false값을 리턴해주며 작동하고 add의 경우에는 예외처리를 발생시켜 작동에 실패하게 된다.

 

출처:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html

 

Queue (Java SE 11 & JDK 11 )

A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the opera

docs.oracle.com

https://pythonq.com/so/java/197653

'알고리즘' 카테고리의 다른 글

동적계획법  (0) 2022.02.15