How to retrieve elements from sorted TreeSet using Binary Search?

前端 未结 3 859
春和景丽
春和景丽 2021-01-14 10:20

I am trying to merge multiple sorted lists into one TreeSet.. And then I am thinking to apply Binary Search algorithm on that TreeSet to retrieve the element in O(log n) tim

3条回答
  •  甜味超标
    2021-01-14 11:12

    TreeSet is backed by a NavigableMap, a TreeMap specifically. Calling contains() on a TreeSet delegates to TreeMap.containsKey(), which is a binary search implementation.

    You can check if an object is contained in the set by using TreeSet.contains(), but you have to have the object first. If you want to be able to look up and retrieve an object, then a Map implementation will be better.

提交回复
热议问题