Page 1 of 1
About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sat Feb 01, 2014 6:30 pm
by icepeanuts
According to API (
http://docs.oracle.com/javase/7/docs/ap ... rator.html), Comparator has 2 methods. But this one only has the implementation of compare(). I understand the equals() of this interface is equal to Object's equals(), but not 100% sure if this class should implement its own equals() as it is defined in the interface.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sun Feb 02, 2014 9:12 pm
by admin
All classes get equals and hashCode methods from Object class. So it is not necessary to define them in every class.
But yes, it is a good idea to implement both to ensure consistency of equals as well as compare().
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sat Jun 28, 2014 10:42 am
by profnotime
Note that this guarantees that the return value will be >= 0 if and only if the key is found.
Why then does
Code: Select all
Arrays.binarySearch(sa, "c", new MyStringComparator());
return 0 when "c" is not in the array of items?
Answer:
This is because the comparator compares length, not actual values.
Therefore, the position for an item with length one is zero.
Replacing "c" with any one length string will yield the same result e.g. "q", "=", "+"
I hope this saves someone a few minutes

Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Mon Nov 09, 2015 2:08 am
by Venceslas
Hello,
I was in doubt about this because sort has not been called before binary search. Maybe it will worth the effort to add this call in the example?
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Mon Nov 09, 2015 9:08 am
by admin
It is not necessary to call sort if you know that the list is already sorted.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed Nov 11, 2015 2:29 am
by Venceslas
Humm, actually I'm not sure the list is sorted.
Indeed:
String[] sa = { "d", "bbb", "aaaa" };
Arrays.sort(sa);
System.out.format("%s %s %s%n", sa[0],sa[1],sa[2] ); => aaaa bbb d
Did I miss something please?
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed Nov 11, 2015 3:05 am
by admin
For the binarySearch method to work, the list has to be sorted as per the Comparator that is being used to search.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed Nov 11, 2015 3:12 am
by Venceslas
Oups, thank you.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed Dec 23, 2015 6:38 am
by Sergiy Romankov
Could you please explain?
I believed that if there no such element in array the return value must be less than 0, but
in this case :
Arrays.binarySearch(sa, "c", new MyStringComparator());
return value is 0.
Yes Comparator compares length of Strings, but shouldn`t it be negative ?
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed Dec 23, 2015 11:38 am
by admin
You are right. If there is no such element in the array the return value must be less than 0.
But "c" is there in the array sa, as per MyStringComparator.