comparable

Why does Collections.sort call Comparator twice with the same arguments?

你离开我真会死。 提交于 2020-05-11 09:07:49
问题 I'm running an example to understand the behavior of Comparator in Java. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class HDTV { private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } } class

Why does Collections.sort call Comparator twice with the same arguments?

余生颓废 提交于 2020-05-11 09:07:36
问题 I'm running an example to understand the behavior of Comparator in Java. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class HDTV { private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } } class

Implements Comparable to get alphabetical sort with Strings

随声附和 提交于 2020-03-18 12:22:47
问题 I would like an object to be comparable (to use it in a TreeSet in that case). My object got a name field and I would like it to be sorted by alphabetical order. I thought first that I could use the unicode value of the string and simply do a subtraction, but then AA would be after Ab for example… Here’s how I started : public final class MyObject implements Comparable<MyObject> { private String name; public MyObject(String name) { this.name = name; } public String name() { return name; }

Implements Comparable to get alphabetical sort with Strings

…衆ロ難τιáo~ 提交于 2020-03-18 12:20:51
问题 I would like an object to be comparable (to use it in a TreeSet in that case). My object got a name field and I would like it to be sorted by alphabetical order. I thought first that I could use the unicode value of the string and simply do a subtraction, but then AA would be after Ab for example… Here’s how I started : public final class MyObject implements Comparable<MyObject> { private String name; public MyObject(String name) { this.name = name; } public String name() { return name; }

Java的Comparable接口的一个陷阱

寵の児 提交于 2020-02-29 11:54:51
Java的Comparable接口提供一个对实现了这个接口的对象列表进行排序的办法。原始的排序对于简单的对象来说具有意义,但是当我们面对复杂的面向对象的业务逻辑对象时,事情变得复杂的多。从业务经理的角度来看,一些交易对象的自然顺序可能是按照交易的价值来排序的,但是从系统管理员的角度来看,这个排序的规则可能是交易的速度。所以在大多数情况下,并没有明确的业务领域对象的自然排序规则。 假设我们找到了一个需要排序的类,比如说Campany。我们把公司的offical name作为主关键字,把id作为次要关键字。这个类的实现如下: public class Company implements Comparable<Company> { private final String id; private final String officialName; public Company(final String id, final String officialName) { this.id = id; this.officialName = officialName; } public String getId() { return id; } public String getOfficialName() { return officialName; } @Override public

《Effective JAVA学习笔记》之 compareTo()

南楼画角 提交于 2020-02-29 11:13:48
class Person implements Comparable<Person> { String firstName; String lastName; int birthdate; // Compare by firstName, break ties by lastName, finally break ties by birthdate public int compareTo(Person other) { if (firstName.compareTo(other.firstName) != 0) return firstName.compareTo(other.firstName); else if (lastName.compareTo(other.lastName) != 0) return lastName.compareTo(other.lastName); else if (birthdate < other.birthdate) return -1; else if (birthdate > other.birthdate) return 1; else return 0; } } 总是实现泛型版本 Comparable 而不是实现原始类型 Comparable 。因为这样可以节省代码量和减少不必要的麻烦。 只关心返回结果的正负号(负/零/正)

equals() vs compareTo() in Comparator/able (Theoretical)

我的未来我决定 提交于 2020-02-28 07:35:06
问题 I don,t understand Javadoc: The natural ordering for a class C is said to be consistent with equals if and only if (e1.compareTo((Object)e2) == 0) has the same boolean value as e1.equals((Object)e2) for every e1 and e2 of class C. Why should be that way? I understand that e1.equals(e2)=true should always imply e1.compareTo(e2)==0, but I cannot understand why the opposite should be true. Compareness is not equalness! 2 equal objects should be compared to zero, but 2 differents ones should be

Why does `Class<T> == Boolean.class` cause a compiler error when `T extends Comparable<? super T>`?

不羁的心 提交于 2020-02-25 02:16:19
问题 I'm using generics to abstract out Comparable data types, as illustrated in the code supplied below. This case arose from a Java Swing component, specifically from attempting to adapt table model to use generics. I have a working solution to that problem (Case 3A below). However, during the process of arriving at that solution I became confused by the behavior of the compiler when I changed the class signature from T extends Comparable<T> to T extends Comparable<? super T> . Why does the

Why does `Class<T> == Boolean.class` cause a compiler error when `T extends Comparable<? super T>`?

心不动则不痛 提交于 2020-02-25 02:15:16
问题 I'm using generics to abstract out Comparable data types, as illustrated in the code supplied below. This case arose from a Java Swing component, specifically from attempting to adapt table model to use generics. I have a working solution to that problem (Case 3A below). However, during the process of arriving at that solution I became confused by the behavior of the compiler when I changed the class signature from T extends Comparable<T> to T extends Comparable<? super T> . Why does the

Sort an ArrayList by an object's attribute in java [duplicate]

怎甘沉沦 提交于 2020-01-30 08:40:43
问题 This question already has answers here : Sorting an ArrayList of objects using a custom sorting order (10 answers) Closed 5 months ago . I wish to sort my objects in order of their Email address. This is the method I've attempted but it does not work, but I'm not even sure it's the correct way to do what I want? public static ArrayList<Billing> sortedListByEmail(ArrayList<Billing> Billing) { ArrayList<Billing> Sort = new ArrayList<Billing>(); for (int i = 0; i < Sort.size(); i++) {