tag:blogger.com,1999:blog-8712770457197348465.post8797099670535401043..comments2024-03-19T05:51:39.935-07:00Comments on Javarevisited: How to override compareTo method in Java - Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-8712770457197348465.post-76858281099837878132021-08-09T02:27:36.521-07:002021-08-09T02:27:36.521-07:00Java 8 way of chaining comparator is much better a...Java 8 way of chaining comparator is much better and less error prone, you can also compare in the reverse order of one property using thenComparing(), here is an example:<br /><br />Comparator.comparing(Order::getName).thenComparing(Order::getAge, Comparator.reverseOrder());<br /><br />This comparator first compare name and then reverse order of age so that young people will appear firstAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12379103244098869102021-02-19T07:22:48.423-08:002021-02-19T07:22:48.423-08:00Hello @Unknown, can you tell bit more? what have y...Hello @Unknown, can you tell bit more? what have you tried so far and what problem you are getting?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14585685993624317752021-02-14T12:06:05.725-08:002021-02-14T12:06:05.725-08:00i tried this multiple times and it still isnt work...i tried this multiple times and it still isnt working but i think thats because i cant return a doubleAnonymoushttps://www.blogger.com/profile/13324487718211030447noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83466067643248767102016-10-11T08:22:25.645-07:002016-10-11T08:22:25.645-07:00goodgoodAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14655636812008384142014-06-12T05:49:46.136-07:002014-06-12T05:49:46.136-07:00@p saikumar - Its less than and great than@p saikumar - Its less than and great thanAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63332356579912509952014-05-22T00:34:30.508-07:002014-05-22T00:34:30.508-07:00in implementation of compareTo(),
in this line ...in implementation of compareTo(),<br />in this line <br />return (this.id < otherStudent.id ) ? -1: (this.id > otherStudent.id) ? 1:0 ;<br /><br />what is lt and gtAnonymoushttps://www.blogger.com/profile/02348099259723817239noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11092713651660848862014-03-14T23:35:23.016-07:002014-03-14T23:35:23.016-07:00You can use Double.CompareTo() method, also you ca...You can use Double.CompareTo() method, also you can use relational operator with double values e.g. <br /><br />return this.doubleValue > o.doubleValue? 1 : this.doubleValue< o.doubleValue? -1 : 0;javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-60373296451566177072014-03-14T23:34:19.996-07:002014-03-14T23:34:19.996-07:00How can I compare double values inside compareTo m...How can I compare double values inside compareTo method for sorting purpose? Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6506201598709188262014-01-26T16:23:42.631-08:002014-01-26T16:23:42.631-08:00who is calling compareTo() method in our programwho is calling compareTo() method in our programkammiti krishnanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58382681946539767792013-09-11T04:37:46.961-07:002013-09-11T04:37:46.961-07:00@srinivas jalappagari : Another possible solution
...@srinivas jalappagari : Another possible solution<br /><br /> Set set = new HashSet();<br /> for (String a : s) {<br /> set.add(a);<br /> }<br /> System.out.println(set);Anonymoushttps://www.blogger.com/profile/16034326079254777233noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52015308361333361452013-08-11T05:19:03.793-07:002013-08-11T05:19:03.793-07:00@srinivas jalappagari :try this
String[] s = new...@srinivas jalappagari :try this<br /><br />String[] s = new String[] { "india", "america", "japan", "america",<br /> "china", "india" };<br /> // HashMap hs=new HashMap();<br /> List hs = new ArrayList();<br /> for (String aa : s) {<br /> if (!hs.contains(aa)) {<br /> hs.add(aa);<br /> }<br /> }<br /> System.out.println(&Anonymoushttps://www.blogger.com/profile/17495757004713739604noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57743616823935377492013-05-20T01:47:50.849-07:002013-05-20T01:47:50.849-07:00isn't it in above code, compareTo() method is ...isn't it in above code, compareTo() method is not consistent with equals(). Since compareTo() only using id, while equals() using all three properties, it may be possible that two student with same id, but different name. Which means compareTo() will return zero, but equals will return false, i.e. compareTo is not consistent with equals Gorbachaunoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-40879607958443443742013-05-09T21:48:49.086-07:002013-05-09T21:48:49.086-07:00Tell me the follow of compareTo(object obj) which ...Tell me the follow of compareTo(object obj) which object is acts as current object is that first element in the array list & second element acts as specified object.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50155167361350408702013-04-16T22:43:22.999-07:002013-04-16T22:43:22.999-07:00Hi Experts,
I want to know how to eliminate duplic...Hi Experts,<br />I want to know how to eliminate duplicate strings in an array and print unique values<br />example:String[] s=new String[]{"india","america","japan","america","china","india"}<br />Output should be:india,america,japan,chinaAnonymoushttps://www.blogger.com/profile/09076137940987797548noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29102718996103702472012-11-23T07:35:42.455-08:002012-11-23T07:35:42.455-08:00Good article, although worth noting that neither o...Good article, although worth noting that neither of the code examples override compareTo (in Object).<br /><br />i.e. <br />compareTo (Student OtherStudent)<br />does not override <br />compareTo (Object o) in the Object class.<br /><br />Both Object and Student implement the Comparable interface.<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-40575534814098393462012-10-19T23:42:32.840-07:002012-10-19T23:42:32.840-07:00String [] arrayA = {"Andrei","Rene&...String [] arrayA = {"Andrei","Rene""Xena"};<br />String [] arrayB = {"Diana","Madonna","Tana"};<br />String [] arrayC = {arrayA.length+arrayB.length}<br /><br />What i am trying to do is to compare first Anderei and Dianathen Andrei comes first.Diana and Rene Diana comes first.then Rene and Madonna Madonna comes first and so on?<br /><brAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15024933542551673972012-05-20T23:35:20.539-07:002012-05-20T23:35:20.539-07:00well compareTo is entirely different than equals()...well compareTo is entirely different than equals() and hashCode. CompareTo is all about order rather than equality and that's why comparison in CompareTo is much trickier than in equals even for equal object.ViraSundarnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20648820026244151722012-04-01T04:24:50.019-07:002012-04-01T04:24:50.019-07:00String [] arrayA = {"Rene","Andrei&...String [] arrayA = {"Rene","Andrei","Xena"};<br />String [] arrayB = {"Diana","Tana","Madonna"};<br /><br />compareTo and get alphabetical order?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18617285931160527592012-01-02T22:44:03.670-08:002012-01-02T22:44:03.670-08:00I too made the mistake of using subtraction for co...I too made the mistake of using subtraction for comparing integers inside compareTo() in Java luckily it never failed and I just realized this after reading this post. I think its very common mistake while overriding compareTo in Java.Joshinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-60553090245117691802011-11-08T20:27:51.309-08:002011-11-08T20:27:51.309-08:00One computing mistake in my post: l - r == -2^32 +...One computing mistake in my post: l - r == -2^32 +1 == 1. Two grammar mistakes in my post: modulo, not module, and its logic, not it's.Eric Jablowhttps://www.blogger.com/profile/16327238795785012303noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15947463702902242002011-11-08T20:09:33.448-08:002011-11-08T20:09:33.448-08:00Hi Eric, You are absolutely correct. Thanks for br...Hi Eric, You are absolutely correct. Thanks for bringing this important point while overriding compareTo. I think using logical operator for comparison makes lot of sense here.Javin @ outofmemory in Javahttp://javarevisited.blogspot.com/2011/09/javalangoutofmemoryerror-permgen-space.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39593367132060670362011-11-08T16:33:31.229-08:002011-11-08T16:33:31.229-08:00It's dangerous to use subtraction for comparin...It's dangerous to use subtraction for comparing int variables. Remember, all arithmetic on integers is module 2^32. Let l = Integer.MIN_VALUE. Let r = Integer.MAX_VALUE. Then, l - r == -2^31 - (2^31 - 1) == -2^32 + 1. But this overflows, and so in Java, l - r ==2, and so this comparison has l > r.<br /><br />In ordinary cases where the integers are reasonable measurements--human agesEric Jablowhttps://www.blogger.com/profile/16327238795785012303noreply@blogger.com