• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Whats wrong with this code

 
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<Student> studentList =new ArrayList<Student>();
studentList.add(new Student("Mateen",77));
studentList.add(new Student("Hassan",87));
studentList.add(new Student("Adnan",17));
studentList.add(new Student("Bilal",2));

//this line is causing an error
Collections.binarySearch(studentList, new Student("Mateen",77));

 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry discovered the problem

Collections.binarySearch(studentList, new Student("Mateen",17),new StudentComparer());

the binary search method requires a custom Comparator for defined objects. Another question why do we need a Comparator, why does the equals method not work ???
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sajjad,

Collections.binarySearch() requires that 1) either elements in the List implement Comparable and are mutually Comparable, or 2) provide a Comparator.
Your original error is because Student is not implementing Comparable.
It also requires that the List be sorted before searching, which you haven't done. That's because binary search algorithm is like that: Divide the list into 2 halves, see which half the element falls in, then divide that half into 2 halves, see which half.....and so on.
Why is equals not sufficient? Because when it divides the list into two, it needs to know in which half - lower or upper - the searched element belongs. equals alone can't tell that.

Cheers
Karthik


 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Karthik for the useful summary.

Jim ... ...
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sajjad,

I have read this article when i was confused with the concept of comparable and comparator
when to use comparable and when to use comparator.

Check out this tutorial

http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic