About Question enthuware.ocpjp.v7.2.1307 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

About Question enthuware.ocpjp.v7.2.1307 :

Post 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.
Last edited by icepeanuts on Sat Feb 08, 2014 2:35 am, edited 1 time in total.

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post 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.

profnotime
Posts: 3
Joined: Sat Jun 28, 2014 10:29 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post 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 :)

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post 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?

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post by admin »

It is not necessary to call sort if you know that the list is already sorted.

HTH,
Paul.

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post 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?

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post by admin »

For the binarySearch method to work, the list has to be sorted as per the Comparator that is being used to search.

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post by Venceslas »

Oups, thank you.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post 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 ?

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1307 :

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests