-
Queue의 add와 offer의 차이언어/자바 2023. 12. 20. 01:15
add vs offer
Queue는 요소의 추가를 위해 'add'와 'offer' 2가지 메소드를 제공한다.(공식문서)
boolean offer(Object o) {}
void add(Object o) throws Exception {}offer는 실패시 false를 반환하지만, add는 throw를 던진다.
비슷하게는 poll과 remove가 있다.사용의 유연성을 위해 2가지의 메소드를 제공하는 것 같지만, 기본적으로 offer와 poll을 사용하는 편이 안정적일 것 같다.
- Exception: add, remove, element
- Safe: offer, poll, peek
Deque는?
데크도 queue와 마찬가지로 add, remove, get을 사용하는 경우는 Exception을 던지고, offer, poll, peek를 사용하는 경우는 에러를 발생시키지 않는다.
- Exception: addFirst, addLast, removeFirst, removeLast, getFirst, getLast
- Safe: offerFirst, offerLast, pollFirst, pollLast, peekFirst, peekLast
'언어 > 자바' 카테고리의 다른 글
Java I/O <1 - OutputStream> (0) 2023.12.23 너무 다양한 List를 만드는 방법들.. (1) 2023.12.20 instanceof는 어떻게 작동하나? (0) 2023.12.20 동적바인딩과 @Override (0) 2023.12.20 자바코드는 어떻게 실행되는가? <3> (0) 2023.12.18