使用Java Stream API根据缩写对用户进行排序。如果缩写重复,请根据用户的年龄进行排序。他们俩都以降序排序
我正在尝试根据用户的缩写对用户进行排序,当重复缩写时,例如“ Alex Fay”和“ Alba Finn”,我必须根据他们的年龄下降顺序对其进行分类。
这是我当前的代码:
List<SortedUser> sortedUserList = list.stream()
.sorted((a, b) -> {
int first = a.getName().charAt(0) + a.getLastName().charAt(0);
int second = b.getName().charAt(0) + b.getLastName().charAt(0);
if(first > second) {
return -1;
} if(first < second) {
return 1;
}
return b.getAge() - a.getAge();
}).collect(Collectors.toList());
此代码的问题是,当我具有缩写“ ar”和“ en”时,它们的总和相同(147
) - 因此, first = =第二
,它们以不正确的顺序出现。 我还试图将初始名字和姓氏初始名称分开比较,但我仍然无法正常工作。
为了按照我想要的方式对它们进行分类,我该更改什么?
样本输入:
(name=Alexandre, lastName=Wuckert, age=66),
(name=Allan, lastName=Wehner, age=84),
(name=Bradley, lastName=Thompson, age=78),
(name=Bernice, lastName=Schoen, age=63)
样本输出:
(name=Bradley, lastName=Thompson, age=78),
(name=Bernice, lastName=Schoen, age=63),
(name=Allan, lastName=Wehner, age=84),
(name=Alexandre, lastName=Wuckert, age=66)
I am trying to sort users based on their initials, and when initials are repeated, say 'Alex Fay' and 'Alba Finn', I have to sort them based on their age in descending order.
This is my current code:
List<SortedUser> sortedUserList = list.stream()
.sorted((a, b) -> {
int first = a.getName().charAt(0) + a.getLastName().charAt(0);
int second = b.getName().charAt(0) + b.getLastName().charAt(0);
if(first > second) {
return -1;
} if(first < second) {
return 1;
}
return b.getAge() - a.getAge();
}).collect(Collectors.toList());
The problem with this code is that when I have initials 'AR' and 'EN', their sums are the same (147
) - therefore first == second
, and they appear one after the other in incorrect order.
I also tried to compare the first name initial and the last name initial separately, but I still couldn't get it to work.
What can I change in order to sort them the way I want it?
Sample input:
(name=Alexandre, lastName=Wuckert, age=66),
(name=Allan, lastName=Wehner, age=84),
(name=Bradley, lastName=Thompson, age=78),
(name=Bernice, lastName=Schoen, age=63)
Sample output:
(name=Bradley, lastName=Thompson, age=78),
(name=Bernice, lastName=Schoen, age=63),
(name=Allan, lastName=Wehner, age=84),
(name=Alexandre, lastName=Wuckert, age=66)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加字符进行排序是不可靠的,因为许多对可以总和到相同的值。因此,您应该构建一串缩写而不是添加它们。然后在您的排序方法中使用以下内容。
以上将根据缩写以降序排序。如果结果== 0,则将使用年龄(如您以前一样)。
注意:您也可以做
second.compareto(first)
,然后交换1和-1
或交换不平等标志< /代码>。这对您来说更有意义。
这是您考虑的其他一些选择。我使用记录保存用户数据。
sort
方法,打印
注意,您也可以将列表对位进行排序,而不使用
stream
,只要列表可变。这里使用了先前定义的比较器
。注意:也可以将比较代码直接放入
sort
方法中。但是,将其分开定义可以重复使用,并且可以使溪流构造不那么混乱。Adding characters to sort on is unreliable since many pairs can sum to the same value. So you should build a String of initials rather than add them. Then use the following in your sort method.
The above will sort in descending order based on the initials. If result == 0 then the ages will be used (as you did before).
Note: you could also do
second.compareTo(first)
and then either swap1 and -1
or swap theinequality signs
. Which ever makes more sense to you.Here are some other alternatives for your consideration. I used a record to hold the User data.
sort
methodprints
Note that you could also just sort the list in place and not use
streams
as long as the list is mutable. Here the previously definedComparator
is used.Note: It is also possible to put the Comparator code directly into the
sort
method. But defining it separately lends itself to reuse and can make the stream construct less cluttered.要使代码更可读取且易于错误,请使用
比较器
接口的Java 8静态方法,而不是将条件链放入lambda表达式中。这是实现A 比较器的另一种方法,它将根据名字的第一个字母进行比较用户,然后用姓氏的名字来比较订单:
您可以将几个比较器定义为
public static
在用户类中的字段,并在需要以特定方式对这些对象进行排序或比较这些对象时重复使用它们。To make the code more readable and less error-prone, use Java 8 static methods of the
Comparator
interface instead of placing a chain of conditions into a lambda expression.Here's another way of implementing a comparator that will compare users based on the first letter of the first name, then by the first letter of the last name and finally by age in descending order:
You can define a couple of comparators as
public static
fields inside the user class and reuse them, when you need to sort or compare these objects in a specific way.