compareto

compareTo with primitives -> Integer / int

荒凉一梦 提交于 2019-11-28 18:06:09
Is it better to write int primitive1 = 3, primitive2 = 4; Integer a = new Integer(primitive1); Integer b = new Integer(primitive2); int compare = a.compareTo(b); or int primitive1 = 3, primitive2 = 4; int compare = (primitive1 > primitive2) ? 1 : 0; if(compare == 0){ compare = (primitive1 == primitive2) ? 0 : -1; } I think the second one is better, should be faster and more memory optimized. But aren't they equal? Peter Lawrey For performance, it usually best to make the code as simple and clear as possible and this will often perform well (as the JIT will optimise this code best). In your

How does the compareTo() method, compare strings? [closed]

不打扰是莪最后的温柔 提交于 2019-11-28 05:19:31
问题 Such as if I were to compare the Strings "Hello" and "World". How does it know Hello is greater than World? The only thing I can come up with is, maybe it uses the ASCII Table as reference? Thanks for the help! 回答1: it compares two strings lexographically . check here in the String API. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one

Why is compareTo on an Enum final in Java?

拟墨画扇 提交于 2019-11-28 04:26:51
An enum in Java implements the Comparable interface. It would have been nice to override Comparable 's compareTo method, but here it's marked as final. The default natural order on Enum 's compareTo is the listed order. Does anyone know why a Java enums have this restriction? For consistency I guess... when you see an enum type, you know for a fact that its natural ordering is the order in which the constants are declared. To workaround this, you can easily create your own Comparator<MyEnum> and use it whenever you need a different ordering: enum MyEnum { DOG("woof"), CAT("meow"); String sound

Equals and Comparable with Sets

和自甴很熟 提交于 2019-11-27 16:23:32
问题 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

compareTo with primitives -> Integer / int

妖精的绣舞 提交于 2019-11-27 11:03:35
问题 Is it better to write int primitive1 = 3, primitive2 = 4; Integer a = new Integer(primitive1); Integer b = new Integer(primitive2); int compare = a.compareTo(b); or int primitive1 = 3, primitive2 = 4; int compare = (primitive1 > primitive2) ? 1 : 0; if(compare == 0){ compare = (primitive1 == primitive2) ? 0 : -1; } I think the second one is better, should be faster and more memory optimized. But aren't they equal? 回答1: For performance, it usually best to make the code as simple and clear as

What does the String classes compareTo() method return

徘徊边缘 提交于 2019-11-27 08:37:13
问题 In the Java API on oracles website: "compareTo Returns: "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument." " Here is an if statement: String a = "abd"; String b = "abc"; if(a.compareTo(b) >= 1) returns true since string a is greater, lexicographically. My question is, does the compareTo always return

How to simplify a null-safe compareTo() implementation?

我只是一个虾纸丫 提交于 2019-11-26 23:24:38
I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): public class Metadata implements Comparable<Metadata> { private String name; private String value; // Imagine basic constructor and accessors here // Irrelevant parts omitted } I want the natural ordering for these objects to be: 1) sorted by name and 2) sorted by value if name is the same; both comparisons should be case-insensitive. For both fields null values are perfectly acceptable, so compareTo must not break in these cases. The

How to sort an array of objects containing null elements?

删除回忆录丶 提交于 2019-11-26 23:14:07
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 value of the int . There are less entries in the .txt file then there are indices in the array so the array ends up looking something like this: fClasses[0] { str1, str2, str3, int1, int [] {1,2,3,4,5}} fClasses[1] { str1, str2, str3, int1, int [] {1,2,3,4,5}} fClasses[2] { str1, str2, str3, int1, int [] {1,2,3,4,5}} fClasses[3] null fClasses[4]

BigDecimal equals() versus compareTo()

江枫思渺然 提交于 2019-11-26 17:17:42
Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BigDecimal x = new BigDecimal("1"); BigDecimal y = new BigDecimal("1.00"); System.out.println(x.equals(y)); System.out.println(x.compareTo(y) == 0 ? "true": "false"); } } You can (consciously) say that x is equal to y (not object reference), but when you run the program, the following result shows: false true Question: What's the difference between compareTo() and equals() in

How do I write a compareTo method which compares objects?

你。 提交于 2019-11-26 16:25:23
I am learning about arrays, and basically I have an array that collects a last name, first name, and score. I need to write a compareTo method that will compare the last name and then the first name so the list could be sorted alphabetically starting with the last names, and then if two people have the same last name then it will sort the first name. I'm confused, because all of the information in my book is comparing numbers, not objects and Strings. Here is what I have coded so far. I know it's wrong but it at least explains what I think I'm doing: public int compare(Object obj) // creating