File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes TreeSets and binary searches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "TreeSets and binary searches" Watch "TreeSets and binary searches" New topic

TreeSets and binary searches

Leo Velazquez

Joined: Apr 27, 2006
Posts: 13
Is there a way to do a binary search in Java 1.4 on a TreeSet using the
same criterion that was used in it's Comparator as a search key without
resorting to Arrays.binarySearch(MyTreeSet.asArray(),string_criterion,
myComparator)? If not what about in Java 1.5 or 1.6?
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
If you execute contains(), it is using binary search by virtue of TreeSet.

From javadoc:
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

[ March 20, 2007: Message edited by: Chu Tan ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Is there some reason you don't just call TreeSet.contains()?

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: TreeSets and binary searches
It's not a secret anymore!