Questions tagged [compareto]

The CompareTo method is found in the .NET Framework and Java and imposes the natural ordering of a class. This is done by returning a negative integer, zero, or a positive integer as the instance of the implementing class is less than, equal to, or greater than the object it is compared to.

The CompareTo method is found in the .NET Framework and Java and imposes a natural ordering of a class. This is done by returning a negative integer, zero, or a positive integer as the instance of the implementing class is less than, equal to, or greater than the object it is compared to.

727 questions
20
votes
5 answers

Technique to automatically check consistency of equals, hashCode, and compareTo?

I'm well aware of the contractual needs to make sure that hashCode is consistent with equals and that equals is consistent with compareTo. However, this is often violated in practice. Are there any tools, techniques, or libraries that can test for…
Michael McGowan
  • 6,528
  • 8
  • 42
  • 70
18
votes
4 answers

How to write hashCode method for a particular class?

I'm trying to generate a hashCode() method for my simple class but i'm not getting anywhere with it. I would appreciate any help. I've implemented the equals() method, which looks as follows, and would also like to know if I need to implement…
A Gore
  • 1,870
  • 2
  • 15
  • 26
15
votes
5 answers

How to sort an array of objects containing null elements?

In my program an array fClasses of fixed length [7] of objects is created, each object is a class FClass that contains 3 Strings, an int, and an int[]. These values are read from a .txt file and added to a specific index of the array based on the…
FergusMacNamara
  • 153
  • 1
  • 1
  • 5
12
votes
3 answers

How does compareTo work?

I know that compareTo returns a negative or positive result on how well one string correlates to the other, but then why: public class Test { public static void main(String[] args) { String y = "ab2"; if(y.compareTo("ac3") == -1)…
Harsh
  • 121
  • 1
  • 1
  • 3
12
votes
5 answers

FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS

I am clueless here... 1: private static class ForeignKeyConstraint implements Comparable { 2: String tableName; 3: String fkFieldName; 4: 5: public int compareTo(ForeignKeyConstraint o) { 6: if…
Trick
  • 3,779
  • 12
  • 49
  • 76
12
votes
4 answers

Cannot invoke compareTo(double) on the primitive type double

The line return array[index1].compareTo(array[index2]); provides an error "Cannot invoke compareTo(double) on the primitive type double". How to solve this issue? /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ /*:: This…
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
12
votes
2 answers

Implementing equals method using compareTo

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…
AnthonyW
  • 1,910
  • 5
  • 25
  • 46
12
votes
6 answers

What integer does DateTime.CompareTo actually return?

I have been looking for an answer for some time now, but nowhere could I actually find it. I was especially looking at this page. There it says that the CompareTo method returns an integer indicating if it is earlier, the same, or later. I…
philkark
  • 2,417
  • 7
  • 38
  • 59
11
votes
1 answer

Implementing custom comparison with CustomComparison and CustomEquality in F# tuple

I'm here to ask a specific topic - I really found few info about this on the web. I'm implementing a F# version of Minimax algorithm. The problem I'm having now is that I want to compare Leaf of my tree (data structure below). Searching the erros…
Pedro Dusso
  • 2,100
  • 9
  • 34
  • 64
11
votes
3 answers

In what way do relational operators not obey the compareTo contract with floating point values?

Quoted from Effective Java - Second Edition by Joshua Bloch For floating-point fields, use Double.compare or Float.compare in place of the relational operators, which do not obey the general contract for compareTo when applied to floating point…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
11
votes
1 answer

Equals and Comparable with Sets

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…
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
10
votes
6 answers

Java - compareTo and operators

If I have a class Person that implements Comparable (compares personA.height to personB.height, for example), is it possible to use personA < personB as a substitute for personA.compareTo(personB) == -1? Are there any issues in doing…
Chet
  • 1,209
  • 1
  • 11
  • 29
10
votes
3 answers

Effective compareTo() for primitive long

Working on a sorted list I came to a point I needed to implement a compareTo() function for primitive long values. I'm not looking for the obvious naive implementation, but was wondering if there's an elegant one-liner code to do that (without…
traveh
  • 2,700
  • 3
  • 27
  • 44
10
votes
2 answers

Does java.util.concurrent.Delayed really force me to violate equals/compareTo consistency?

Trying to use Java's DelayQueue, I have to implement the Delayed interface which requires a compareTo() "method that provides an ordering consistent with its getDelay method.". The intention is of course that the DelayQueue can easily sort the…
Harald
  • 4,575
  • 5
  • 33
  • 72
9
votes
5 answers

Understanding TreeSet when compareto returns 0

I have created a Student class like this: public class Student implements Comparable { private String firstName; private String lastName; public Student(String firstName, String lastName) { this.firstName = firstName; …
learner
  • 6,062
  • 14
  • 79
  • 139
1
2
3
48 49