- 需求描述:电商订单数据处理,根据下⾯的list1和list2 共11个订单
统计出同时被两个⼈购买的商品列表(交集)
统计出两个⼈购买商品的差集
统计出全部被购买商品的去重并集
统计两个⼈的分别购买订单的平均价格
统计两个⼈的分别购买订单的总价格
public class VideoOrder {
private String tradeNo;
private int money;
private String title;
public VideoOrder(String tradeNo, String title, int money) {
this.tradeNo = tradeNo;
this.title = title;
this.money = money;
}
public String getTradeNo() {
return tradeNo;
}
public void setTradeNo(String tradeNo) {
this.tradeNo = tradeNo;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof VideoOrder){
VideoOrder o1 = (VideoOrder)obj;
return title.equals(o1.title);
}
return super.equals(obj);
}
@Override
public int hashCode() {
return title.hashCode();
}
@Override
public String toString() {
return "VideoOrder{" +
"title='" + title + '\'' +
'}';
}
}
public class Main {
public static void main(String[] args) throws Exception {
//总价 35
List<VideoOrder> videoOrders1 = Arrays.asList(
new VideoOrder("20190242812", "springboot教程", 3),
new VideoOrder("20194350812", "微服务SpringCloud", 5),
new VideoOrder("20190814232", "Redis教程", 9),
new VideoOrder("20190523812", "网页开发教程", 9),
new VideoOrder("201932324", "百万并发实战Netty", 9));
//总价 54
List<VideoOrder> videoOrders2 = Arrays.asList(
new VideoOrder("2019024285312", "springboot教程", 3),
new VideoOrder("2019081453232", "Redis教程", 9),
new VideoOrder("20190522338312", "网页开发教程", 9),
new VideoOrder("2019435230812", "Jmeter压力测试", 5),
new VideoOrder("2019323542411", "Git+Jenkins持续集成", 7),
new VideoOrder("2019323542424", "Idea全套教程", 21));
//两个订单平均价格
double videoOrderAvg1 = videoOrders1.stream().collect(Collectors.averagingInt(VideoOrder::getMoney)).doubleValue();
System.out.println("订单列表1平均价格="+videoOrderAvg1);
double videoOrderAvg2 = videoOrders2.stream().collect(Collectors.averagingInt(VideoOrder::getMoney)).doubleValue();
System.out.println("订单列表2平均价格="+videoOrderAvg2);
//订单总价
int totalMoney1 = videoOrders1.stream().collect(Collectors.summingInt(VideoOrder::getMoney)).intValue();
System.out.println("订单列表1总价="+totalMoney1);
int totalMoney2 = videoOrders2.stream().collect(Collectors.summingInt(VideoOrder::getMoney)).intValue();
System.out.println("订单列表2总价="+totalMoney2);
//交集
List<VideoOrder> intersectionList = videoOrders1.stream().filter(videoOrders2::contains).collect(Collectors.toList());
System.out.println(intersectionList);
//差集1
List<VideoOrder> diffList1 = videoOrders1.stream().filter(obj->!videoOrders2.contains(obj)).collect(Collectors.toList());
System.out.println("差集1="+diffList1);
List<VideoOrder> diffList2 = videoOrders2.stream().filter(obj->!videoOrders1.contains(obj)).collect(Collectors.toList());
System.out.println("差集2="+diffList2);
//并集
List<VideoOrder> allVideoOrder = videoOrders1.parallelStream().collect(Collectors.toList());
allVideoOrder.addAll(videoOrders2);
System.out.println("并集="+allVideoOrder);
List<VideoOrder> allVideoOrderDistinct = allVideoOrder.parallelStream().distinct().collect(Collectors.toList());
System.out.println("去重并集="+allVideoOrderDistinct);
}
}