Java Stream 使用收集器按两个值进行过滤
我有下面的工作代码,使用一个值进行比较,但想与两个值进行比较。我需要再添加一个过滤器作为“getAddressID”以及 getAction。有人可以帮我修改下面的代码吗?
List<Data> finalData = dataList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(Data::getAction))),
ArrayList::new))
public class TestCollection {
/**
* @param args
*/
public static void main(String[] args) {
List<Data> dataList = new ArrayList<Data>();
Data data1 = new Data("DELIVERED", "1112");
Data data2 = new Data("DELIVERED", "1113");
Data data3 = new Data("PICKEUP", "1112");
Data data4 = new Data("PICKEUP", "1112");
dataList.add(data1);
dataList.add(data2);
dataList.add(data3);
dataList.add(data4);
//Filer by Action
List<Data> finalData = dataList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<> (Comparator.comparing(Data::getAction))), ArrayList::new));
for(Data data : finalData) {
System.out.println(" Action " + data.getAction() + " Address ID " + data.getAddressID());
}
}
结果
是: “操作已交付地址 ID 1112” “Action PICKEUP Address ID 1112”
但我想要结果为: “已交付地址 ID 1112” “已交付地址 ID 1113” “取货地址 ID 1112”
I have the below working code using one value to compare but want to compare with two values. I need to add one more filter as "getAddressID" along with getAction. Could some help me to modify the below code?
List<Data> finalData = dataList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(Data::getAction))),
ArrayList::new))
public class TestCollection {
/**
* @param args
*/
public static void main(String[] args) {
List<Data> dataList = new ArrayList<Data>();
Data data1 = new Data("DELIVERED", "1112");
Data data2 = new Data("DELIVERED", "1113");
Data data3 = new Data("PICKEUP", "1112");
Data data4 = new Data("PICKEUP", "1112");
dataList.add(data1);
dataList.add(data2);
dataList.add(data3);
dataList.add(data4);
//Filer by Action
List<Data> finalData = dataList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<> (Comparator.comparing(Data::getAction))), ArrayList::new));
for(Data data : finalData) {
System.out.println(" Action " + data.getAction() + " Address ID " + data.getAddressID());
}
}
}
The result is :
"Action DELIVERED Address ID 1112"
"Action PICKEUP Address ID 1112"
But I want result as:
"DELIVERED Address ID 1112"
"DELIVERED Address ID 1113"
"PICKEUP Address ID 1112"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 thenComparing 链接比较器,即
You can chain your comparators using
thenComparing
, ie如果我猜对了,您将尝试通过两个属性
addressID & 的组合从列表中获取不同的项目。行动。 这里是一个可接受的答案,感谢@Stuart Marks,它致力于通过一个属性获取不同的元素。可以进一步修改以解决您的用例,例如:
方法
distinctByKeys
可能看起来像If I get it right, you are trying to get distinct items from your list by the combination of the two properties
addressID & action
. Here is a an accepted answer, thanks to @Stuart Marks, which works to get distinct elements by one property. That could be further modified to solve your use case like:where the method
distinctByKeys
could look like