愿景:"让编程不再难学,让技术与生活更加有趣"
更多架构课程请访问 xdclass.net
目录
第1集 Collection和Lambda电商数据处理实战需求说明
第1集 Collection和Lambda电商数据处理实战需求说明
简介:综合jdk8新特性,collection和lambda完成数据处理需求整理
需求描述:电商订单数据处理,根据下面的list1和list2 各10个订单
-
统计出同时被两个人购买的商品列表(交集)
-
统计出两个人购买商品的差集
-
统计出全部被购买商品的去重并集
-
统计两个人的分别购买订单的平均价格
-
统计两个人的分别购买订单的总价格
//总价 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));
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;
}
}
第2集 JDK8新特性处理电商订单数据之答案讲解上集
简介:讲解使用jdk8新特性完成订单需求一、二、三
@Override
public boolean equals(Object obj) {
if(obj instanceof VideoOrder) {
VideoOrder o1 = (VideoOrder)obj;
return title.equals(o1.getTitle());
}
return super.equals(obj);
}
@Override
public String toString() {
return "VideoOrder{" +
"money=" + money +
", title='" + title + '\'' +
'}';
}
@Override
public int hashCode() {
return title.hashCode();
}
//交集
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);
//差集2
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.stream().distinct().collect(Collectors.toList());
System.out.println("去重并集 = "+allVideoOrderDistinct);
第3集 JDK8新特性处理电商订单数据之答案讲解下集
简介:讲解使用jdk8新特性完成订单需求四、五
//两个订单平均价格
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();
int totalMoney2 = videoOrders2.stream().collect(Collectors.summingInt(VideoOrder::getMoney)).intValue();
System.out.println("订单列表1总价="+totalMoney1);
System.out.println("订单列表2总价="+totalMoney2);
干货文档
关注公众号发送:“CSDN干货文档” 即可领取