Tips1;字符串不要用这种方式判空,应为默认"" 代表非空
Optional.ofNullable(AAA).orElse(BBB) AAA对象不为空,返回AAA,否则返回BBB对象
Optional.ofNullable(AAA).orElseGet(DDD) AAA对象不为空,返回AAA,否则执行DDD方法
Optional.ofNullable(AAA).ifPresent(CCC) AAA对象不为空,则执行CCC方法(执行完无返回值void)
Optional.ofNullable(AAA).map(FFF) AAA对象不为空,则执行FFF方法(执行完有返回值R)例子1:name不为空时,将他的值赋值给frame对象的busiName属性
String name = "哈哈哈"
ComcCFrameModel frame = new ComcCFrameModel();
Optional.ofNullable(name).ifPresent(frame::setBusiName);//Java8中 :: 的含义和用法 https://blog.csdn.net/qq_46249770/article/details/123805088
参考:
https://www.jianshu.com/p/34536591289b https://wenku.baidu.com/view/7d5e1d88d2f34693daef5ef7ba0d4a7302766c83.html https://blog.csdn.net/ykko2009/article/details/122616191
list.stream().map()用法详解
例子2:集合流
addFramePages.stream().map(ComcCFramePage::getPageId).collect(Collectors.toList());参考:
https://blog.csdn.net/weixin_43466040/article/details/122494328 https://www.runoob.com/java/java8-streams.html
List<User> userList = new ArrayList<User>();
userList.add(new User(3, "Jack","上海"));
userList.add(new User(1, "Many","广州"));
userList.add(new User(2, "Amy","上海"));
userList.add(new User(4, "Lily","北京"));
//排序
userList = userList.stream().sorted(Comparator.comparing(User::getId).reversed()).collect(Collectors.toList());
//输出指定内容值
List<String> dd = userList.stream().map(User::getName).collect(Collectors.toList()); //map用于单个字段(单个字段可做运算)的输出
//过滤(不为空的、偶数、奇数、或者其他条件)
userList = userList.stream().filter(t->t.getId()>=1).collect(Collectors.toList());
//循环
userList.stream().forEach(item->{
long fd = item.getId();
String fff = item.getName();
//做其他操作
});
//匹配(判断)
boolean flag2 = userList.stream().anyMatch(item -> item.getName().indexOf("an")>0); //集合里面有某个值包含an字符的,就返回true
//集合分组
Map<String, List<User>> groupAd = userList.stream().collect(Collectors.groupingBy(User::getAddress));
//集合分组排序
Map<String, List<User>> groupSort = userList.stream().sorted(Comparator.comparing(User::getId)).collect(Collectors.groupingBy(User::getAddress));