merge
This commit is contained in:
@@ -118,6 +118,8 @@ public class CollectionDemo {
|
||||
* lambda表达式
|
||||
*/
|
||||
list.forEach(o -> System.out.println(o));
|
||||
//删除某元素
|
||||
list.removeIf(integer -> integer == 0);
|
||||
|
||||
/**
|
||||
* 迭代器
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.xwbing.demo;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.domain.entity.sys.SysUser;
|
||||
import com.xwbing.domain.entity.sys.SysUserRole;
|
||||
import com.xwbing.service.sys.SysUserRoleService;
|
||||
@@ -38,16 +39,23 @@ public class LambdaDemo {
|
||||
// //遍历
|
||||
// Arrays.stream(arrays).forEach(System.out::println);
|
||||
list.forEach(System.out::println);
|
||||
lists.sort((o1, o2) -> o1 - o2);//升序排序,不需要收集
|
||||
System.out.println("sort:" + lists.stream().sorted((o1, o2) -> o2 - o1).collect(Collectors.toList()));//排序
|
||||
System.out.println("map:" + lists.stream().map(o1 -> o1 * 2).collect(Collectors.toList()));//转换
|
||||
System.out.println("distinct:" + lists.stream().distinct().collect(Collectors.toList()));//去重(去重逻辑依赖元素的equals方法)
|
||||
System.out.println("limit:" + lists.stream().limit(4).collect(Collectors.toList()));//截取
|
||||
System.out.println("skip:" + lists.stream().skip(4).collect(Collectors.toList()));//丢弃
|
||||
//匹配
|
||||
boolean b1 = lists.stream().anyMatch(o -> o == 1);
|
||||
boolean b = lists.stream().allMatch(o -> o == 1);
|
||||
boolean b2 = lists.stream().noneMatch(o -> o == 1);
|
||||
//过滤
|
||||
System.out.println("filter:" + lists.stream().filter(o1 -> o1 > 3 && o1 < 8).collect(Collectors.toList()));
|
||||
Predicate<Integer> gt = integer -> integer > 3;//函数式接口Predicate
|
||||
Predicate<Integer> lt = integer -> integer < 8;
|
||||
System.out.println("重用filter:" + lists.stream().filter(gt.and(lt)).collect(Collectors.toList()));
|
||||
//删除
|
||||
lists.removeIf(item -> item > 3);//根据条件删除,不用收集
|
||||
//聚合
|
||||
System.out.println("reduce:" + lists.stream().reduce((o1, o2) -> o1 + o2).get());//聚合
|
||||
System.out.println("reduce:" + lists.stream().reduce(0, (o1, o2) -> o1 + o2));//聚合(给定默认值)
|
||||
@@ -69,6 +77,8 @@ public class LambdaDemo {
|
||||
System.out.println("all:" + lists.stream().filter(Objects::nonNull).distinct().mapToInt(num -> num * 2).skip(2).limit(4).sum());
|
||||
//遍历list存入map里
|
||||
Map<String, SysUser> userMap = new SysUserService().listAll().stream().collect(Collectors.toMap(SysUser::getId, Function.identity()));
|
||||
Map<String, String> nameMap = new SysUserService().listAll().stream().collect(Collectors.toMap(SysUser::getId, SysUser::getName));
|
||||
Map<String, String> jsonMap = new ArrayList<JSONObject>().stream().collect(Collectors.toMap(o1 -> o1.getString(""), o2 -> o2.getString("")));
|
||||
//分组
|
||||
Map<String, List<SysUser>> groupMap = new SysUserService().listAll().stream().collect(Collectors.groupingBy(SysUser::getSex));//(分组条件为key,分组成员为value)
|
||||
//非空判断
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
package com.xwbing.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DemoService {
|
||||
public static void main(String[] args) {
|
||||
List<Integer> list = new ArrayList<Integer>();// 有序
|
||||
list.add(1);
|
||||
list.add(2);
|
||||
list.add(3);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
list.add(6);
|
||||
System.out.println("filter:" + list.stream().filter(o1 -> o1 > 3 && o1 < 8).collect(Collectors.toList()));
|
||||
list.removeIf(o1 -> o1 > 3 && o1 < 8);
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user