comparable

What do the return values of Comparable.compareTo mean in Java?

↘锁芯ラ 提交于 2019-12-29 02:32:28
问题 What is the difference between returning 0 , returning 1 and returning -1 in compareTo() in Java? 回答1: Official Definition From the reference docs of Comparable.compareTo(T): Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must

Implementing Comparable with a generic class

我怕爱的太早我们不能终老 提交于 2019-12-28 12:32:29
问题 I want to define a class that implements the generic Comparable interface. While in my class I also defined a generic type element T . In order to implement the interface, I delegate the comparison to T . Here is my code: public class Item<T extends Comparable<T>> implements Comparable<Item> { private int s; private T t; public T getT() { return t; } @Override public int compareTo(Item o) { return getT().compareTo(o.getT()); } } When I try to compile it, I get the following error information:

Compare two objects with “<” or “>” operators in Java

蓝咒 提交于 2019-12-28 03:05:42
问题 How to make two objects in Java comparable using "<" or ">" e.g. MyObject<String> obj1= new MyObject<String>(“blablabla”, 25); MyObject<String> obj2= new MyObject<String>(“nannaanana”, 17); if (obj1 > obj2) do something. I've made MyObject class header as public class MyObject<T extends Comparable<T>> implements Comparable<MyObject<T>> and created method Comp but all the gain I got is now I can use "sort" on the list of objects, but how can I compare two objects to each other directly? Is if

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

Having trouble implementing a nested class comparator

余生颓废 提交于 2019-12-25 18:54:46
问题 I'm implementing a Point class and trying to use a nested class to implement a comparator, which will compare based on the slope of two points. I'm having trouble implementing such comparator and don't understand how to use it in my main() function. This is the error message I got when I try to compile it: Point.java:20: error: non-static variable this cannot be referenced from a static context if (Point.this.slopeTo(pt1) > Point.this.slopeTo(pt2)) { ^ Point.java:20: error: non-static

Sorting ArrayList<Object> by Comparable Interface

帅比萌擦擦* 提交于 2019-12-25 11:14:09
问题 Actually I want to sort Array List of objects. I am using Comparable interface for this purpose. It is completely working But the problem is that when I am sorting is is giving these two problems. All name who have 1st letter capital are coming together on the top and all name who have 1st letter small are coming together on the bottom. All the sorted Capital letters word are coming together after that all the small letter words are coming at bottom together. Here is my bean MyShares.java

Implementing Binary Tree in Java with Generic Comparable<T> data?

不问归期 提交于 2019-12-25 07:05:19
问题 Q: In my implementation of a binary tree below, why does the compiler choke at if (data.compareTo(this.data) <= 0) , producing Error: incompatible types: java.lang.Comparable<T> cannot be converted to T ? Both data and this.data are of type Comparable<T> and should be able to use or be an argument to the compareTo() method...right? Well, clearly not. But I really don't understand why. Generics are still baffling me. public class MyBinaryTreeNodeG<T>{ Comparable<T> data; MyBinaryTreeNodeG<T>

Java Sort algorithm String with Number

风格不统一 提交于 2019-12-25 05:15:03
问题 I have a List of Strings. EXAMPLE_1, EXAMPLE_2, EXAMPLE_3 ... EXAMPLE_99 What is the best algorithm for sorting here? Is it possible with a Collator? This is my current procedure, but I guess there could be a better way: public class Example implements Comparable<Example> { private final String id; public getId() { return id; } private Integer getIdNo() { try { return Integer.parseInt(getId().replaceAll("[\\D]", "")); } catch (NumberFormatException e) { return null; } } @Override public int

Java Sort algorithm String with Number

橙三吉。 提交于 2019-12-25 05:14:02
问题 I have a List of Strings. EXAMPLE_1, EXAMPLE_2, EXAMPLE_3 ... EXAMPLE_99 What is the best algorithm for sorting here? Is it possible with a Collator? This is my current procedure, but I guess there could be a better way: public class Example implements Comparable<Example> { private final String id; public getId() { return id; } private Integer getIdNo() { try { return Integer.parseInt(getId().replaceAll("[\\D]", "")); } catch (NumberFormatException e) { return null; } } @Override public int

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