compareto

Effective compareTo() for primitive long

╄→尐↘猪︶ㄣ 提交于 2020-01-11 08:22:08
问题 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 creating a new Long(value)). Maybe something like this: @Override public int compareTo(MyClass that) { return (int) ((value - that.value) >>> 32); } Can anyone verify that would work and/or suggest another implementation? 回答1: One liner code to that:

How to use the Comparable CompareTo on Strings in Java

旧时模样 提交于 2020-01-10 09:24:55
问题 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; 回答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

CompareTo Overide Sort

一笑奈何 提交于 2020-01-06 07:23:52
问题 I am having an issue overiding a compareTo methods. The program simulates different employee types, and I have it sorting by employee type perfectly, but can not get it to do a secondary sort by gross pay. Once it sorts by class name/employee type, it then needs to sort by grossPay, which I can obtain by a helper method. Below is the code: public int compareTo(Object o) { Employee other = (Employee) o; if(other instanceof Salaried) return -1; else if(other instanceof Daily) return 1; else

Java compareTo for String and Integer arguments

对着背影说爱祢 提交于 2020-01-02 08:57:53
问题 I am implementing the bubble sort algorithm and I want it to be able to accept both Integer and String parameters. I cast all input as Strings and use the compareTo method to compare the integers casted as strings to the strings. I am getting an incorrect answer when using compareTo to compare the casted integers. What am I doing wrong? 回答1: Integer.compareTo sorts numbers numerically. This is what you want. String.compareTo sorts strings lexicographically; that is, in alphabetical order. I

Understanding TreeSet when compareto returns 0

不羁岁月 提交于 2019-12-30 07:18:12
问题 I have created a Student class like this: public class Student implements Comparable<Student> { private String firstName; private String lastName; public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } // Getters & Setters follow here... @Override public int compareTo(Student student) { int hash = this.firstName.compareTo(student.firstName); return hash; } @Override public String toString() { return "Student [firstName=" + firstName + ",

How do I write a compareTo method which compares objects?

旧城冷巷雨未停 提交于 2019-12-27 11:46:21
问题 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

using compareTo method when overriding compareTo?

。_饼干妹妹 提交于 2019-12-25 09:24:29
问题 when implements Comparable interface and override compareTo method, @Override public int compareTo(Name o) { int val = this.name.compareTo(o.name); if (val != 0) { return val; } if (count != o.count) { return count - o.count; } } The third line, I realized that I can use compareTo when I override it, and it automatically compares things follows the natural order. But isn't compareTo an abstract method in the comparable interface. Without defining it, it still does compare? Also, why I do not

When to include what?

浪子不回头ぞ 提交于 2019-12-25 00:23:20
问题 I created a class Person (as the book says) to hold the name and last name of a person entered from the keyboard and then there is another class PhoneNumber which encapsulates the country code, area code and the number of a person as a String. Person is intended to be used as the key in a Hashmap. Class BookEntry encapsulates both Person and PhoneNumber . A lot of BookEntry objects make up a HashMap that represents a phonebook. Person implements Comparable<Person> so it contains CompareTo

Char cannot be dereferenced? using compareTo

流过昼夜 提交于 2019-12-24 15:27:13
问题 I'm trying to make a program that will read in a string and compare each character in the string to see if it is in alphabetical order. public class Main { public static void Main ( String[] args) { System.out.println("#Please enter the string: "); String s = BIO.getString(); while(!s.equals("END")){ int length = s.length(); String sLC = s.toLowerCase(); int count = 0; boolean inOrder = true; for(int i = 0; i < length - 1 ; i++){ if(sLC.charAt(i).compareTo(sLC.charAt(i+1)) > 0) { inOrder =

Comparing two dates using Comparable interface

安稳与你 提交于 2019-12-24 01:37:32
问题 I am currently designing a GUI for a bank database application. In my GUI I have a "List accounts opened before" button that I am trying to code to list all of the accounts in the database in a text area that have dates before a date that the user inputs into a text field. I am very confused about the implementation behind a Comparable interface and how to correctly compare two dates in a array of objects. In my mind my ShowBefore methods logic is correct, however I think that is not the case