lambda表达式现在属于程序员必备的编码技巧了。
简单的map,filter,分组等就不说了。
lambda表达式复合排序:
使用thenComparing
public static void main(String[] args) throws Exception {
List<SysUser> list = new ArrayList<>();
list.add(new SysUser(1L,"张三",2L,"admin",new Date()));
list.add(new SysUser(2L,"李莫愁",3L,"common",new Date()));
list.add(new SysUser(3L,"梅超风",2L,"admin",new Date()));
list.add(new SysUser(4L,"张三丰",4L,"user",new Date()));
list.add(new SysUser(5L,"张无忌",3L,"common",new Date()));
/**
* 按照roleId 降序排列, 如果roleId相同,则按照UserId升序排列.
*/
List<SysUser> collect = list.stream()
.sorted(
Comparator.comparing(SysUser::getRoleId)
.reversed()
.thenComparing(Comparator.comparing(SysUser::getUserId)))
.collect(Collectors.toList());
}
这样排序出来类似于sql中的 ORDER BY roleId DESC,userId ASC 这种了。
有时候分页在sql层面做不了,比如有些字段是统计出来的,统计后又根据其他条件进行查询。
public static void main(String[] args) throws Exception {
List<SysUser> list = new ArrayList<>();
list.add(new SysUser(1L,"张三",2L,"admin",new Date()));
list.add(new SysUser(2L,"李莫愁",3L,"common",new Date()));
list.add(new SysUser(3L,"梅超风",2L,"admin",new Date()));
list.add(new SysUser(4L,"张三丰",4L,"user",new Date()));
list.add(new SysUser(5L,"张无忌",3L,"common",new Date()));
int pageNum = 1; //当前页
int pageSize = 3; //一页显示3条
Page<SysUser> page = new Page<>(pageNum, pageSize);
page.setTotal(list.size());
List<SysUser> collect = list.stream()
.skip((pageNum - 1) * pageSize) //使用skip 跳过指定条数
.limit(pageSize) //使用limit取指定的条数
.collect(Collectors.toList()); //搜集起来
page.setRecords(collect);
}
public static void main(String[] args) throws Exception {
List<SysUser> list = new ArrayList<>();
list.add(new SysUser(1L,"张三",2L,"admin",new Date()));
list.add(new SysUser(2L,"李莫愁",3L,"common",new Date()));
list.add(new SysUser(3L,"梅超风",2L,"admin",new Date()));
list.add(new SysUser(4L,"张三丰",4L,"user",new Date()));
list.add(new SysUser(5L,"张无忌",3L,"common",new Date()));
Long idSum = list.stream().map(SysUser::getUserId).reduce(0L,Long::sum);
}
异步查询使用到CompletableFuture和Stream.of
public static void main(String[] args) throws Exception {
List<SysUser> list = new ArrayList<>();
list.add(new SysUser(1L,"张三",2L,"admin",new Date()));
list.add(new SysUser(2L,"李莫愁",3L,"common",new Date()));
list.add(new SysUser(3L,"梅超风",2L,"admin",new Date()));
list.add(new SysUser(4L,"张三丰",4L,"user",new Date()));
list.add(new SysUser(5L,"张无忌",3L,"common",new Date()));
//在list中异步过滤出 角色id大于2的数据
CompletableFuture<List<SysUser>> future = CompletableFuture.supplyAsync(() -> {
return list.stream().filter(sysUser -> sysUser.getRoleId() > 2).collect(Collectors.toList());
});
//异步future 完成后 继续过滤出 名称中包含张三的数据
CompletableFuture<List<SysUser>> async = future.thenApplyAsync(list1 -> {
return list1.stream().filter(sysUser -> sysUser.getUserName().contains("张三")).collect(Collectors.toList());
});
//在list中异步过滤出用户类型为admin的数据
CompletableFuture<List<SysUser>> completableFuture = CompletableFuture.supplyAsync(() -> {
return list.stream().filter(sysUser -> sysUser.getUserType().equals("admin")).collect(Collectors.toList());
});
//主线程等待 异步处理合并结果
List<Object> futureList = Stream.of(async, completableFuture).map(CompletableFuture::join).collect(Collectors.toList());
List<SysUser> list1 = (List<SysUser>)futureList.get(0);
List<SysUser> list2 = (List<SysUser>)futureList.get(1);
/**
* list1为角色id大于2,且名称里包含张三的
* list2为用户类型为admin的数据。
* 异步获取可以提高接口响应时间
*/
//其他处理...
}