如何覆盖 compareTo (Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42863823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to override compareTo (Java)
提问by aLittleMind
I'm a beginner in programming and I have two classes. First class is:
我是编程初学者,我有两门课。第一课是:
public class User implements Comparable<User>
with field int age
, constructor and overrided method of interface Comparable:
带有字段int age
、构造函数和接口 Comparable 的重写方法:
@Override
public int compareTo(User user) {
return user.age >= age ? -1 : 0;
}
Second class is
public class SortUser
with a method to make a Set collection from a List:
第二个类是
public class SortUser
使用从列表创建 Set 集合的方法:
public Set<User> sort(List<User> list) {
Set<User> result = new TreeSet<>();
for (User user : list) {
result.add(user);
}
return result;
}
It seems to me that all User
objects in a Set should be sorted, but when I made a List with 3 User
objects...
在我看来,User
集合中的所有对象都应该排序,但是当我创建一个包含 3 个User
对象的列表时......
User a = new User(1);
User b = new User(2);
User c = new User(3);
List<User> list = new ArrayList<>();
list.add(c);
list.add(a);
list.add(b);
(Now the list's order is: 312
)
...and created a Set
(TreeSet
) from that list:
(现在列表的顺序是:)312
...并从该列表中创建一个Set
( TreeSet
):
SortUser sortUser = new SortUser();
Set<User> set = sortUser.sort(list);
At the end I have a set
with that order: 13
, it means that only two objects are in the set
. What is going wrong?
最后我有一个set
具有该顺序的:13
,这意味着只有两个对象在set
. 出了什么问题?
采纳答案by Roma Khomyshyn
As I see you have wrong implementation of compare method. Could you update it to?
正如我所见,您对比较方法的实现有误。你能更新到吗?
@Override
public int compareTo(User user) {
return Integer.compare(age, user.age);
}
回答by Carcigenicate
What you're doing with the TreeSet
is unnecessary. I'm not sure they're guaranteed to have certain ordering when iterated.
你在做什么TreeSet
是不必要的。我不确定它们在迭代时是否保证有一定的顺序。
Just replace your sort method with
只需将您的排序方法替换为
Collections.sort(list)
Collections.sort(list)
And my guess as to why an element is being dropped is your compareTo
method never returns a 1
in any case, so elements are always considered to be less than or equal to other elements, which is probably screwing with the TreeSet
.
我对为什么删除元素的猜测是您的compareTo
方法1
在任何情况下都不会返回 a ,因此元素始终被认为小于或等于其他元素,这可能与TreeSet
.
回答by rathna
User class
用户类
public class User implements Comparable<User>{
int age;
User(int age){age=age;}
@Override
public int compareTo(User user) {
return this.age >= age ? -1 : 0;
}
}
prepare list
准备清单
User a = new User(1);
User b = new User(2);
User c = new User(3);
List<User> list = new ArrayList<>();
list.add(c);
list.add(a);
list.add(b);
for sorting
用于排序
Set<User> list1 = new TreeSet(list);
回答by Lova Chittumuri
Please follow below methodology
请遵循以下方法
In case of string.
在字符串的情况下。
public static Comparator<Employee> NameComparator = new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
return e1.getName().compareTo(e2.getName());
}
};
In case of Integer values
在整数值的情况下
public static Comparator<Employee> SalaryComparator = new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
return (int) (e1.getSalary() - e2.getSalary());
}
};
回答by Айгуль Камалова
class Scratch {
public static void main(String[] args) {
List<User> list = new ArrayList<>();
list.add(new User(3));
list.add(new User(1));
list.add(new User(2));
Collections.sort(list);
list.forEach(el -> System.out.println(el.age));
}
}
class User implements Comparable<User> {
int age;
User(int age) {
this.age = age;
}
@Override
public int compareTo(User user) {
return this.age >= user.age ? -1 : 0;
}
}