JAVA8新特性Optional和Stream和Localdate用法

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.csdn.net/kk514020/article/details/103511507

https://blog.csdn.net/qq_35970057/article/details/109050817

https://blog.csdn.net/gejiangbo222/article/details/106123636?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_v2~rank_aggregation-6-106123636.pc_agg_rank_aggregation&utm_term=ifpresent%E7%94%A8%E6%B3%95&spm=1000.2123.3001.4430

https://blog.csdn.net/weiqiang915/article/details/107209981

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值