Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSets and binary searches

 
Leo Velazquez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some reason you don't just call TreeSet.contains()?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic