aspose file tools*
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
Author

TreeSets and binary searches

Leo Velazquez
Greenhorn

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
    
    1
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
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSets and binary searches