강의

멘토링

커뮤니티

Inflearn Community Q&A

wsde43665's profile image
wsde43665

asked

Mastering Java Basics with Eunjong (Do it Java Programming Introduction) - Part 2 (Mastering Edition)

Finding the position of a number in a sorted number (binary search)

이진 탐색 문제 질문드립니다

Written on

·

223

0

이진 탐색 문제에서, 자바의 TreeSet이 레드-블랙 트리 알고리즘으로 구현된걸로 알고있는데, 그냥 TreeSet을 생성해서 값을 넣고 contains로 검색하면 되는거랑 선생님이 직접 코드로 푼거랑 뭐가 다른걸까요??

java객체지향알고리즘

Answer 1

0

eunjong님의 프로필 이미지
eunjong
Instructor

Set의 경우는 key 값만을 사용하고 key값은 colleciton으로 반환 받아서, JDK 내부의 contatins() 코드는 아래와 같습니다. 하나의 iteration을 돌면서 체크하는 것은 크게 다르지 않습니다.

public boolean contains(Object o) {

Iterator<E> it = iterator();

if (o==null) {

while (it.hasNext())

if (it.next()==null)

return true;

} else {

while (it.hasNext())

if (o.equals(it.next()))

return true;

}

return false;

}

wsde43665's profile image
wsde43665

asked

Ask a question