compareto

Java, how to use compareTo to sort an Arraylist

心已入冬 提交于 2019-11-30 18:13:02
问题 Im trying to figure out how to sort an ArrayList using comparable, my code looks like this: public class playerComparsion{ public static void main(String[] args){ ArrayList<Object> list = new ArrayList<Object>(); Player p1 = new Players(1,92,Zlatan); Player p2 = new Players(2,92,Hazard); Player p3 = new Players(1,82,Klose); list.add(p1); list.add(p2); list.add(p3); } } class Players implements Comparable{ int position; String name; int rating; public Players(int i, int j, String string) {

Implementing equals method using compareTo

徘徊边缘 提交于 2019-11-30 18:03:27
General question: When implementing an override of the default equals method in Java, what concerns should I have about simply utilizing an already implemented compareTo method vs writing independent logic into the equals method? I noticed someone mention in another question that foo.equals((String)null) returns false whereas String.compareTo((String)null) throws a NullPointerException . What makes these inconsistent results ideal functionality? Sample equals method: @Override public boolean equals(Object obj) { if (obj != null && obj instanceof MyClass) { MyClass msg = (MyClass)obj; return

Undocumented String.compareTo(null) NPE?

隐身守侯 提交于 2019-11-30 16:24:58
问题 The following little test throws an NPE: public class Test { public static void main(String[] args) { String a = "a"; String b = null; System.out.println(a.compareTo(b)); } } Yet, the Javadoc of compareTo() does not mention that the parameter cannot be null . This is strange, since Javadocs usually mentions when parameters cannot be null . Is this just a glitch in the documentation or is there a more fundamental reason/twist I am missing? 回答1: You can get some explanation when you look at the

How to use the Comparable CompareTo on Strings in Java

陌路散爱 提交于 2019-11-30 08:19:17
I can use it to sort by emp id but I'm not sure if it is possible to compare strings. I get an error the operator is undefined for strings. public int compareTo(Emp i) { if (this.getName() == ((Emp ) i).getName()) return 0; else if ((this.getName()) > ((Emp ) i).getName()) return 1; else return -1; What you need to use is the compareTo() method of Strings. return this.getName().compareTo(i.getName()); That should do what you want. Usually when implementing the Comparable interface, you will just aggregate the results of using other Comparable members of the class. Below is a pretty typical

What should int compareTo() return when the parameter string is null?

巧了我就是萌 提交于 2019-11-30 07:09:58
问题 It is said that when input parameter is null, compareTo() should throw a NullPointerException. However, I am implementing a class which needs to compare fields with the type of String. These fields need not to be mandatory. I wonder in this case, 1) What should I return when the input is null? Should any not-null strings lexicographically bigger or smaller than null? and 2) If this is considered bad practice, is there any supporting arguments? Should I force the user to use empty strings

Scala idiom for ordering by multiple criteria

我怕爱的太早我们不能终老 提交于 2019-11-30 03:45:44
I want to do something like this: class Foo extends Ordered[Foo] { val x val y val z . . . . def compare(that: Foo) = { val c0 = this.length compareTo that.length // primary comparison lazy val c1 = this.x compareTo that.x // secondary comparison lazy val c2 = this.y.size compareTo that.y.size // tertiary comparison lazy val c3 = this.z.head compareTo that.z.head // final tie breaker if (c0 != 0) c0 else if (c1 != 0) c1 else if (c2 != 0) c2 else if (c3 != 0) c3 else c4 } } I was wondering if there was any cleaner way to write this kind of thing. I am expecting some thing like Ordering

Implementing equals method using compareTo

别等时光非礼了梦想. 提交于 2019-11-30 01:51:35
问题 General question: When implementing an override of the default equals method in Java, what concerns should I have about simply utilizing an already implemented compareTo method vs writing independent logic into the equals method? I noticed someone mention in another question that foo.equals((String)null) returns false whereas String.compareTo((String)null) throws a NullPointerException . What makes these inconsistent results ideal functionality? Sample equals method: @Override public boolean

Equals and Comparable with Sets

烈酒焚心 提交于 2019-11-29 02:01:14
I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working with to achieve what they wanted. I then got to thinking ... as you do ... that I was eliminating duplicates by returning 0 from the compareTo method, not by returning true from an equals implementation as one would need to do to correctly indicate a duplicate in a Set (from the definition of a Set ). I have no objection to using this technique but

What should int compareTo() return when the parameter string is null?

跟風遠走 提交于 2019-11-29 01:22:51
It is said that when input parameter is null, compareTo() should throw a NullPointerException. However, I am implementing a class which needs to compare fields with the type of String. These fields need not to be mandatory. I wonder in this case, 1) What should I return when the input is null? Should any not-null strings lexicographically bigger or smaller than null? and 2) If this is considered bad practice, is there any supporting arguments? Should I force the user to use empty strings instead? If using empty string, won't that confuse the case in which the field is not applicable and the

Scala idiom for ordering by multiple criteria

六眼飞鱼酱① 提交于 2019-11-29 00:56:20
问题 I want to do something like this: class Foo extends Ordered[Foo] { val x val y val z . . . . def compare(that: Foo) = { val c0 = this.length compareTo that.length // primary comparison lazy val c1 = this.x compareTo that.x // secondary comparison lazy val c2 = this.y.size compareTo that.y.size // tertiary comparison lazy val c3 = this.z.head compareTo that.z.head // final tie breaker if (c0 != 0) c0 else if (c1 != 0) c1 else if (c2 != 0) c2 else if (c3 != 0) c3 else c4 } } I was wondering if