目录
一 案例
1.1 对象转list
1.2 过滤对象
1.3 排序
1.4 匹配
1.5 最大值最小值
1.6 拼接字符串
1.7 求和
1.8 分组
1.9 分组求和
1.10 综合案例
一 案例
1.1 对象转list
/**
* @author admin
对象转map ; mapper层实体类获取,到业务层转换为DTO,
* @return void
*/
public static void duixiangToMap(){
List<User> list = Arrays.asList(
new User(1L, "张三", 18),
new User(2L, "李四", 20),
new User(3L, "王五", 33),
new User(4L, "赵六", 44));
List<UserDTO> userDTOList = list.stream().map(obj -> {
UserDTO userDTO = new UserDTO(obj.getId(), obj.getName(), obj.getAge());
return userDTO;
}).collect(Collectors.toList());
userDTOList.forEach(System.out::println);
}
结果:
1.2 过滤对象
/**
* @author admin
* @description
案例2: 过滤长度大于7
* @return void
*/
public static void duiFilter(){
List<String> list = Arrays.asList( "SpringBoot", "SpringCloud", "Spring", "SpringMVC","mybatis","html", "git" );
List<String> stringList = list.stream().filter(obj -> obj.length() > 7).collect(Collectors.toList());
System.out.println(stringList);
}
1.3 排序
public static void testSorted(){
List<String> list = Arrays.asList(
"SpringBoot", "SpringCloud", "Spring",
"SpringMVC", "mybatis", "html", "git"
);
List<String> collect = list.stream().sorted().collect(Collectors.toList());
System.out.println(collect);
//根据长度进行排序
List<String> collect1 = list.stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList());
System.out.println(collect1);
//limit截取
List<String> collect2 = list.stream().sorted(Comparator.comparing(String::length)).limit(3).collect(Collectors.toList());
System.out.println(collect2);
//limit截取
List<String> collect3 = list.stream().sorted(Comparator.comparing(String::length).reversed()).limit(3).collect(Collectors.toList());
System.out.println(collect3);
}
1.4 匹配
/**
* @author admin
* @description
match 匹配
allMatch检查是否匹配所有元素,符合才返回trueanyMatch检查是否至少匹配一个元素
*/
public static void testAllOrAnyMatch(){
List<String> list = Arrays.asList(
"SpringBoot",
"SpringCloud",
"Spring",
"SpringMVC",
"mybatis",
"html",
"git"
);
boolean allMatch = list.stream().allMatch(obj -> obj.length() > 5);
System.out.println(allMatch);
boolean anyMatch = list.stream().anyMatch(obj -> obj.length() > 10);
System.out.println(anyMatch);
}
1.5 最大值最小值
/**
* @author admin
求最大值和最小值
* @return void
*/
public static void testMaxMin(){
List<User> list = Arrays.asList(
new User(1L, "张三", 18),
new User(2L, "李四", 20),
new User(3L, "王五", 33),
new User(4L, "赵六", 44));
Optional<User> userOptional = list.stream().max(Comparator.comparingInt(User::getAge));
System.out.println(userOptional.get().getAge());
Optional<User> userOptional1 = list.stream().min(Comparator.comparingInt(User::getAge));
System.out.println(userOptional1.get().getAge());
}
1.6 拼接字符串
/**
拼接字符串
*/
public static void joinStr() {
String collect = Stream.of("spt", "smvc","111").collect(Collectors.joining("@" ));
System.out.println("r:"+collect);
}
结果:
1.7 求和
/**
* @author admin
求和
* @return void
*/
public static void qiuHe(){
Integer value = Stream.of(1, 2, 3, 4, 5, 6).reduce((item1,item2) -> item1 + item2).get();
System.out.println(value);
//不适用lambda表达式
Integer value1 = Stream.of(1, 2, 3, 4, 5, 6).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
return integer + integer2; }}
).get();
System.out.println("value1:"+value1);
//10作为初始值,然后和第一个数相加,结果再与第二个数相加,直到所有的数相加完成为止
Integer value3 = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(10, (sum, item) -> sum + item);
System.out.println("value3:"+value3);
}
1.8 分组
public static void qiuByGroup() {
List<String> list = Arrays.asList("SpringBoot", "SpringCloud",
"Spring", "SpringMVC", "mybatis", "html",
"git");
Map<Boolean, List<String>> listMap = list.stream().collect(partitioningBy(obj -> obj.length() > 8));
listMap.forEach((key, value) -> {
System.out.println(key);
value.forEach(System.out::println);
System.out.println();
});
}
1.9 分组求和
public static void qiuByGroupSUm(){
//根据学生的省份进行分组
List<Student> students = Arrays.asList(new Student("⼴东", 23), new Student("⼴东", 24), new Student("⼴东", 23),
new Student("北京", 22), new Student("北京", 20), new Student("北京", 20), new Student("海南", 25));
Map<String, List<Student>> collect = students.stream().collect(Collectors.groupingBy(obj -> obj.getName()));
collect.forEach((key,value)->{ System.out.println(key); value.forEach(System.out::println);});
//统计学生的各个年龄信息
IntSummaryStatistics summaryStatistics = students.stream().collect(Collectors.summarizingInt(Student::getAge));
System.out.println("平均值:"+summaryStatistics.getAverage());
System.out.println("人数:"+summaryStatistics.getCount());
System.out.println("最大值:"+summaryStatistics.getMax());
System.out.println("最小值:"+summaryStatistics.getMin());
System.out.println("总和:"+summaryStatistics.getSum());
}
1.10 综合案例
Java 8 Stream API:强大而灵活的数据处理流水线
public static void testTen(){
//总价 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));
//交集
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> 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);
//两个订单平均价格
double videoOrderAvg1 = videoOrders1.stream().collect(Collectors.averagingInt(VideoOrder::getMoney));
System.out.println("订单1平均价格="+videoOrderAvg1);
double videoOrderAvg2 = videoOrders2.stream().collect(Collectors.averagingInt(VideoOrder::getMoney));
System.out.println("订单2平均价格="+videoOrderAvg2);
//两个订单总价格
long sum1 = videoOrders1.stream().collect(Collectors.summarizingInt(VideoOrder::getMoney)).getSum();
System.out.println("订单1总价格="+sum1);
long sum2 = videoOrders2.stream().collect(Collectors.summarizingInt(VideoOrder::getMoney)).getSum();
System.out.println("订单2总价格="+sum2);
}