This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to use Collections.binarySearch() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to use Collections.binarySearch()" Watch "How to use Collections.binarySearch()" New topic
Author

How to use Collections.binarySearch()

Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
If I use Collections.binarySearch() with list of objects (not String, not Wrapper classes), Do I need to override equals() method??


SCJP 5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

No, String is already implementing the Comparable interface, so you don't need anything. In fact, it has nothing to do with equals(). but with Comparable or Comparator. You either implement Comparable, or you provide your own Comparator and pass it as the third parameter of binarySearch.


[My Blog]
All roads lead to JavaRanch
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Thanks Satou

But what key used to search list of third-party objects?

such as (from K&B book):

ArrayList<DVDInfo> DVDList = new ArrayList<DVDInfo>();

int index = Collections.binarySearch(this.DVDList, this.DVDList.get(0));
System.out.println("found at index: " + index); // work

But when I pass a new instance of DVDInfo

DVDInfo dvd = new DVDInfo("smallvile", "classic", new Date());

int index = Collections.binarySearch(this.DVDList, dvd);
System.out.println("found at index: " + index); // not found, return -3, there are only 2 elements in the list???



DVDInfoList.txt
smallvile, classic, 10/11/06
borat, drama, 02/05/06

thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You're showing the content of a text file, but not the content of the List. Try to dump the list, to make sure that all dvds are in there.
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
here is the outp Output
[smallvile - classic, borat - drama]
After sort: [borat - drama, smallvile - classic]

1st search found at index: 0

2nd search found at index: -3 // why -3 ?? not -1


and I still confuse, when we invoke this statement:

Collections.binarySearch(this.DVDList, this.DVDList.get(0))

is binarySearch method use the same fields/attributes used in compare() method implementation??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use Collections.binarySearch()
 
Similar Threads
List<? extends Comparable<? super T>> list, T key
All the Collections classes implementing List are ordered? - question from examlab
Binary Search
Performance again
Cannot Collections.binarySearch() a collection that implements Comparable.