1.Optional类是Java8为了解决null值判断问题
2.Stream 是Java SE 8类库中新增的关键抽象,Java 8 引入的的Stream主要用于取代部分Collection的操作,
每个流代表一个值序列,流提供一系列常用的聚集操作,可以便捷的在它上面进行各种运算
1.stram()方法:将集合装为流
2.collect()方法:将流转为集合
3.filter()方法:将转为流的集合过滤出满足要求的流
4.map()方法:将每个元素映射成新元素
5.limit(n):获取n个元素
6.skip(n):跳过n元素
7.skip和limit组合实现分页(对数据库的压力没有减轻,只是看着分页了)
8.distinct:去除重复元素
//五个user对象
User user1 = new User(1, "张三", 10);
User user2 = new User(2, "李四", 15);
User user3 = new User(3, "王五", 20);
User user4 = new User(4, "赵六", 25);
User user5 = new User(5, "严七", 30);
//将User对象存入list
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
System.out.println(userList);
//[User{id=1, username='张三', age=10}, User{id=2, username='李四', age=15}, User{id=3, username='王五', age=20}, User{id=4, username='赵六', age=25}, User{id=5, username='严七', age=30}]
//1.stram()方法:将集合装为流
Stream<User> stream = userList.stream();
System.out.println(stream);//打印对象地址。java.util.stream.ReferencePipeline$Head@65f651eb
//2.collect()方法:将流转为集合
List<User> users = userList.stream().collect(Collectors.toList());
System.out.println(users);[User{id=1, username='张三', age=10}, User{id=2, username='李四', age=15}, User{id=3, username='王五', age=20}, User{id=4, username='赵六', age=25}, User{id=5, username='严七', age=30}]
//3.filter()方法:将转为流的集合过滤出满足要求的流
List<User> userList1 = userList.stream().//将集合转为流
filter(user -> user.getAge() > 20).//过滤出年龄大于20的user。(类似于sql中的where user.age > 20)
collect(Collectors.toList());//将流转回集合(便于打印观察结果)
System.out.println(userList1);//[User{id=4, username='赵六', age=25}, User{id=5, username='严七', age=30}]
//4.map()方法:将每个元素映射成新元素
List<User> userList2 = userList.stream().filter(user -> user.getAge() > 20).
//过滤出年龄大于20的user
map(user -> {//将过滤得到的user对象的年龄设置为50
user.setAge(50);//执行你想要的操作,每个元素会映射产生新元素,所以map()方法要有返回值
return user;//返回新元素
}).
collect(Collectors.toList());//将流转为集合
System.out.println(userList2);//[User{id=4, username='赵六', age=50}, User{id=5, username='严七', age=50}]
//5.limit(n):获取n个元素
List<User> userList3 = userList.stream().limit(3).collect(Collectors.toList());//获取前三个元素。类似于mysql数据库中的 'limit 参数一,参数二' 关键字的参数二
System.out.println(userList3);//[User{id=1, username='张三', age=10}, User{id=2, username='李四', age=15}, User{id=3, username='王五', age=20}]
//6. skip(n):跳过n元素
List<User> userList4 = userList.stream().skip(2).collect(Collectors.toList());//跳过两个元素,类似于mysql数据库中 'limit 参数一,参数二' 关键字的参数一
System.out.println(userList4);//[User{id=3, username='王五', age=20}, User{id=4, username='赵六', age=50}, User{id=5, username='严七', age=50}]
//7.skip和limit组合实现分页
List<User> userList5 = userList.stream().skip(2).limit(2).collect(Collectors.toList());//获取第二页数据(每页显示两条数据)
System.out.println(userList5);//[User{id=3, username='王五', age=20}, User{id=4, username='赵六', age=50}]
//8. distinct:去除重复元素
//向集合中插入一个重复元素
userList.add(user5);
System.out.println(userList);//[User{id=1, username='张三', age=10}, User{id=2, username='李四', age=15}, User{id=3, username='王五', age=20}, User{id=4, username='赵六', age=50}, User{id=5, username='严七', age=50}, User{id=5, username='严七', age=30}]
List<User> userList6 = userList.stream().distinct().collect(Collectors.toList());//去重(实体类中需要实现equals()和hashCode())
System.out.println(userList6);//[User{id=1, username='张三', age=10}, User{id=2, username='李四', age=15}, User{id=3, username='王五', age=20}, User{id=4, username='赵六', age=50}, User{id=5, username='严七', age=50}]
3.LocalDate、LocalTime、LocalDateTime区别
LocalDate:年月日
LocalTime:时分秒
LocalDateTime:年月日时分秒
4.实际工作中用法
//判断startTime不为null时把startTime值赋给TserviceStartDate,
Optional.ofNullable(startTime).ifPresent(serviceTimeRecordEntity::setTserviceStartDate);
//获取开始时间最小值
Optional<LocalDate> startTime = items.stream().map(VehicleItemDTO.ServiceItem::getBeginTime).min(LocalDate::compareTo)
5.参考资料
https://blog.youkuaiyun.com/kk514020/article/details/103511507
https://blog.youkuaiyun.com/qq_35970057/article/details/109050817
https://blog.youkuaiyun.com/weiqiang915/article/details/107209981