[英]How to override compareTo (Java)
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...在我看来,Set 中的所有
User
对象都应该排序,但是当我创建一个包含 3 个User
对象的 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);
(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
.最后我有一个
set
与秩序: 13
,这意味着只有两个对象在set
。 What is going wrong?出了什么问题?
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);
}
What you're doing with the TreeSet
is unnecessary.您对
TreeSet
所做的事情是不必要的。 I'm not sure they're guaranteed to have certain ordering when iterated.我不确定它们在迭代时是否保证有一定的顺序。
Just replace your sort method with只需将您的排序方法替换为
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
,因此元素总是被认为小于或等于其他元素,这可能与TreeSet
。
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());
}
};
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);
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;
}
}
You might want to try this:你可能想试试这个:
@Override
public int compareTo(User user) {
if (this == user){
return 0;
}
if (user != null){
return this.age.compareTo(user.getAge())
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.