tag:blogger.com,1999:blog-8712770457197348465.post1989472723151688749..comments2025-01-07T04:58:30.529-08:00Comments on Javarevisited: How to use Comparator and Comparable in Java? With examplejavin paulhttp://www.blogger.com/profile/15028902221295732276[email protected]Blogger37125tag:blogger.com,1999:blog-8712770457197348465.post-62294108847306918822017-04-16T19:35:30.692-07:002017-04-16T19:35:30.692-07:00This is the code second example which is supposed ...This is the code second example which is supposed to compare the person names <br /><br />public class SortByPerson_Name implements Comparator{<br /><br /> public int compare(Object o1, Object o2) {<br /> Person p1 = (Person) o;<br /> Person p2 = (Person) o; <br /> return p1.getName().compareTo(p2.getName());<br /> }<br />}<br />Anonymoushttps://www.blogger.com/profile/03807156568060420177[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-45672401176387838512016-08-06T20:52:31.043-07:002016-08-06T20:52:31.043-07:00Hi ,
I got some important points to note from this...Hi ,<br />I got some important points to note from this article !!<br />In the second example which is supposed to compare the person names why have you compared person ids in SortByPerson_ID class . Please clarify . This question is really bothering me .<br /><br />ThanksAnonymoushttps://www.blogger.com/profile/14900645335094509956[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-9567698862519378722016-07-11T08:35:23.873-07:002016-07-11T08:35:23.873-07:00@Unknown, suppose Order object is from third party...@Unknown, suppose Order object is from third party library, in that case you don't have access to code but you have access to binary.<br /><br />Suppose you want to compare Order by total value, you can write a Total comparator as follows<br /><br />class TotalComparator implements Comparator{<br /> <br /> public int compare(Order o1, Order o2){<br /> return o1.getTotal() - o2.getTotal()javin paulhttps://www.blogger.com/profile/15028902221295732276[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-38911496997307364642016-07-08T01:49:29.379-07:002016-07-08T01:49:29.379-07:00Hi Javin,
2. The Comaprator interface helps you t...Hi Javin,<br /><br />2. The Comaprator interface helps you to compare objects from third party API. you can create a custom comparator and pass it to Collections.sort() method.<br /><br />For this point, can you please provide a coding example as i was asked to write this programme in an interview recently.<br /><br /><br /><br /><br /><br />Anonymoushttps://www.blogger.com/profile/14168699563584782881[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-25293671860197398112014-12-16T23:51:45.133-08:002014-12-16T23:51:45.133-08:00Thank you for such important artical. Sorry to say...Thank you for such important artical. Sorry to say, but it doesn't give the clear cut idea of when to use Comparator or Comparable.Anonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-44389689541728879532014-10-13T12:08:02.602-07:002014-10-13T12:08:02.602-07:00Importance of Comparator interface:
1. The sortin...Importance of Comparator interface:<br /><br />1. The sorting logic can be placed in separate class. So you can create different custom comparator to compare objects based on different attributes.e.g Sorting using id, name etc.<br /><br />2. The Comaprator interface helps you to compare objects from third party API. you can create a custom comparator and pass it to Collections.sort() method.<br /Anonymoushttps://www.blogger.com/profile/17107187605009031183[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-7011072856886472172014-08-15T02:25:54.671-07:002014-08-15T02:25:54.671-07:00@Vitaly, It doesn't required you to have acces...@Vitaly, It doesn't required you to have access of code, if you have classes e.g. inside JAR file, you can code your own comparator which can compare those classes. On the other hand, you can not do the same with Comparable interface, because the class itslef need to be changed to add "implements Comparable" , which will not be possible until you have source file. Anonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-26635649525428414832014-07-30T21:18:37.181-07:002014-07-30T21:18:37.181-07:00"
2) Some time you write code to sort object ..."<br />2) Some time you write code to sort object of a class for which you are not the original author, or you don't have access to code. In these cases you can not implement Comparable and Comparator is only way to sort those objects.<br />"<br /><br />But how I can implement Comparator if i don't have access to code?Vitaly Zdanevichhttps://www.blogger.com/profile/08059131468426079600[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-40789590097502169592014-06-18T00:15:13.046-07:002014-06-18T00:15:13.046-07:00public int compareTo(Object o) {
Person p ...public int compareTo(Object o) {<br /> Person p = (Person) o; <br /> return this.person_id - o.person_id ;<br /> }<br /> ….<br />}<br /><br />Wrong codeAnonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-72698376079320839812013-09-17T23:28:10.423-07:002013-09-17T23:28:10.423-07:00Is there any way to sort the objects in middle out...Is there any way to sort the objects in middle out fashion ,say person ids are 1,2,3,4,5 in order.I want to sort the order in the following way 3,4,2,5,1<br /><br />Anonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-46683591415256895472013-06-04T04:52:37.356-07:002013-06-04T04:52:37.356-07:00return this.person_id - o.person_id ; // this is w...return this.person_id - o.person_id ; // this is wrong....<br /><br />this should be like<br />return this.person_id - p.person_id ; //missing pAnonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-70937450819841074512013-03-17T23:20:34.100-07:002013-03-17T23:20:34.100-07:00I like the article. I already knew some of the bas...I like the article. I already knew some of the basic differences but was looking for more holistic views on this. Now I can feel it. Thanks :) Keep posting more. Anonymoushttps://www.blogger.com/profile/10713599749294943416[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-41044753913062516122013-02-28T08:07:13.729-08:002013-02-28T08:07:13.729-08:00there are some typos in your code
like:
// in com...there are some typos in your code<br />like: <br />// in compareTo<br />return this.person_id - o.person_id ; // it should be p.person_id<br /><br />//in compare <br /> Person p1 = (Person) o; // it should be (Person) o1;<br /> Person p2 = (Person) o; // it should be (Person) o2<br /><br />genexhttps://www.blogger.com/profile/04048345451470573346[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-35704325287908203952013-02-04T17:02:52.607-08:002013-02-04T17:02:52.607-08:00Hi @Anonymous, I think I have given, particularly ...Hi @Anonymous, I think I have given, particularly on When to use Comparator and Comparable in Java. Anyway, Since Comparable is used to defined natural order sorting, You can only sort object one way e.g. either by using id or by using name. Now if you want to sort Person object based on Age, Salary etc. You need to create various Comparator e.g. AgeComparator, SalaryComparator etc. You can Javin @ ClassLoader in Javahttp://javarevisited.blogspot.com/2012/12/how-classloader-works-in-java.html[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-29224402751140259702013-02-04T05:28:31.760-08:002013-02-04T05:28:31.760-08:00Hi Javin, thanks for your post.
But you did not gi...Hi Javin, thanks for your post.<br />But you did not give any scenario in which comparator must be used and comarable will not work or vice-versa. If both can be used in every scenario then why java has provided 2 interfaces resolving the same purpose.?Anonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-81008711056138388332013-01-29T12:52:34.550-08:002013-01-29T12:52:34.550-08:00the casting for comparator example should have
P...the casting for comparator example should have <br /><br />Person p1 = (Person) o1;<br /> Person p2 = (Person) o2; <br /><br />Anonymoushttps://www.blogger.com/profile/00221042103771480765[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-64010608161231672502012-11-12T00:39:29.717-08:002012-11-12T00:39:29.717-08:00In first example it looks like a bug.
return this....In first example it looks like a bug.<br />return this.person_id - o.person_id;<br /><br />It should be:<br />return this.person_id - p.person_id;<br />Anonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-15390142130847185292012-09-01T01:30:30.779-07:002012-09-01T01:30:30.779-07:00@Saga, Thanks to catch that bug, one of the most c...@Saga, Thanks to catch that bug, one of the most common mistake programmers make while overriding equals, compareTo and compare methods, which accept Object as argument. Hope I had used @Override annotation :). Anyway good catch.Javin @ JDBC Best practiceshttp://javarevisited.blogspot.sg/2012/08/top-10-jdbc-best-practices-for-java.html[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-77064318092816642962012-08-31T23:35:16.212-07:002012-08-31T23:35:16.212-07:00Wrong example....
it is compile time error...
ple...Wrong example....<br />it is compile time error...<br /><br />please override compareTo() method dont overload.Anonymoushttps://www.blogger.com/profile/14980607533953354084[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-81659100012681946302012-08-08T04:25:14.386-07:002012-08-08T04:25:14.386-07:00Hi Eric, Thanks for your comment, both of your sug...Hi Eric, Thanks for your comment, both of your suggestions are perfectly valid. In fact not using Integer arithmetic to decide comparison order is what I discussed in my post about <a href="http://javarevisited.blogspot.sg/2011/11/how-to-override-compareto-method-in.html" rel="nofollow">overriding ComparTo method</a>, but given its conciseness, you can use it if its guaranteed that both numbers Javin @ CyclicBarrier Example Javahttp://javarevisited.blogspot.sg/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.html[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-89981354072992743582012-08-07T12:24:18.319-07:002012-08-07T12:24:18.319-07:00In any case, your Person should implement Comparab...In any case, your Person should implement Comparable<Person>, not the raw type Comparable.<br /><br />Furthermore, using subtraction: a-b &lt 0, == 0, or > 0 to determine ordering is generally unreliable. It is possible for a < b but a - b > 0 because integer arithmetic in Java is modulo 2^32. Integer.MIN_VALUE < 1. However, Integer.MIN_VALUE - 1 == Integer_MAX_VALUE, as Eric Jablowhttps://www.blogger.com/profile/16327238795785012303[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-48209570320709093432012-06-06T00:15:32.215-07:002012-06-06T00:15:32.215-07:00Hi Javin,
Thanks for the nice article... but can ...Hi Javin,<br /><br />Thanks for the nice article... but can you please give some examples on what exactly is natural ordering and situations which requires Comparator instead of Comparable in other words where it requires to use Comparator only?Anonymous[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-83737363842200272262012-04-17T07:01:46.904-07:002012-04-17T07:01:46.904-07:00Q. Sayeed, you are correct on that compareTo can b...Q. Sayeed, you are correct on that compareTo can be use to implement any kind of sorting order in Java but standard is to use Comparable to implement natural order sorting of Object, natural order means lexicographic for String, ascending or descending for numeric and dates etc. let me know if you have any other doubt of Comparator and Comparable.Javin @ ArrayList vs LinkedListhttp://javarevisited.blogspot.com/2012/02/difference-between-linkedlist-vs.html[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-8942931876658841112012-04-16T22:18:07.319-07:002012-04-16T22:18:07.319-07:00Hi,
i am not agree with the statement that "C...Hi,<br />i am not agree with the statement that "Comparable in Java is used to implement only natural ordering of object." <br /><br />I think, you can do all type of possible ordering by using Comparable as much as by using Comparator. You didn't think like that.Q.Sayeedhttps://www.blogger.com/profile/05154983919105168944[email protected]tag:blogger.com,1999:blog-8712770457197348465.post-22954632220906356112012-03-20T23:40:31.378-07:002012-03-20T23:40:31.378-07:00while writing d code to write customized logic in ...while writing d code to write customized logic in implementation class i got an excepton like "class,enum,interface expected"<br />how can i solve that problem?madhuhttps://www.blogger.com/profile/12975749322281516291[email protected]