- lambda表达式实际应用
- lambda归纳收集
- lambda查找与匹配方法
- lambda四大内置核心函数式接口
- Lambda实际应用
//lambda根据条件筛选集合中的实体对象
List cacheList = getScoreTypeFromCache();
ScoreTypeBean sc = cacheList.stream().filter(item -> item.getScoreId().equals(scoreId)).findFirst().get();
//lambda根据条件筛选集合中的对象,得到所需要的集合
List cacheList = getScoreTypeFromCache();
List list = cacheList.stream().filter(item -> item.getSysFlag().equals(SystemFlagEnum.INLAY.getFlag())).collect(Collectors.toList());
//提取集合中的某一个字段元素,组成新的集合
List scoreTypeBeanList = iMsScoreTypeConfigService.getScoreIdByScoreCode(list);
List list = scoreTypeBeanList.stream().map(ScoreTypeBean::getScoreId).collect(Collectors.toList());
//求某个对象和
list.stream().mapToInt(DataAnalysisSkuActionDay::getPaymentNum).sum()
//嵌套对象中的某个属性收集 preference entity / id entity code
list.stream().map(preference:: getId).map(id::getcode).distinct.collect(collectors.tolist());
-
lambda归纳收集
@Data
public static class Employee {
Employee(String name, String position, Integer salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
private String name;
private String position;
private Integer salary;
}
public static void main(String[] args) {
List list = Arrays.asList(
new Employee("张三", "普通员工", 2000),
new Employee("李四", "项目经理", 6000),
new Employee("王五", "普通员工", 3000),
new Employee("刘七", "技术总管", 9000),
new Employee("唐八", "普通员工", 2000),
new Employee("钱九", "行政管理", 11000),
new Employee("赵十", "普通员工", 3500),
new Employee("杜十一", "高级工程师", 7800)
);
//max--根据工资找出最大记录
Optional employee = list.stream().max((Comparator.comparing(Employee::getSalary)));
//映射计算最大值
list.stream().mapToLong(Employee::getSalary).max();
//根据职位归纳收集
Map map = list.stream().collect(Collectors.groupingBy(Employee::getPosition));
System.out.println(map);
//collect归纳计算工资总数
double a = list.stream().collect(Collectors.summingDouble(Employee::getSalary));
System.out.println(a);
//接受Collector接口的实现,提供统计函数汇总
LongSummaryStatistics longSummaryStatistics = list.stream().collect(Collectors.summarizingLong(Employee::getSalary));
long h = longSummaryStatistics.getCount();
System.out.println(longSummaryStatistics.getAverage());
System.out.println(longSummaryStatistics.getMin());
}
-
lambda查找与匹配方法
@Data
public static class Employee {
Employee(String name, String position, Integer salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
private String name;
private String position;
private Integer salary;
}
public static void main(String[] args) {
/**
* allMatch -- 检查是否匹配所有元素 -- R:boolean
* anyMatch -- 检查是否至少匹配一个元素 -- R:boolean
* noneMatch -- 检查是否没有匹配所有元素 -- R:Boolean
* findFirst -- 返回第一个元素 -- R:optional
* count -- 返回总元素个数 -- R:long
*
*
*/
List list = Arrays.asList(
new Employee("张三", "普通员工", 2000),
new Employee("李四", "项目经理", 6000),
new Employee("王五", "普通员工", 3000),
new Employee("刘七", "技术总管", 9000),
new Employee("唐八", "普通员工", 2000),
new Employee("钱九", "行政管理", 11000),
new Employee("赵十", "普通员工", 3500),
new Employee("杜十一", "高级工程师", 7800)
);
boolean flag = list.stream().allMatch((e) -> e.getName().equals("王五"));
System.out.println(flag);
flag = list.stream().anyMatch((e) -> e.getName().equals("王五"));
System.out.println(flag);
//工资大于8000的第一个元素
Optional employee = list.stream().filter(e -> e.getSalary() > 8000).findFirst();
System.out.println(employee);
//获取集合总条数---鸡肋,除非某些业务获取过滤后的总条数
long a = list.stream().count();
System.out.println(a);
//获取集合所有工资的总和值
long c = list.stream().mapToLong(Employee::getSalary).sum();
System.out.println(c);
//获取集合中最大工资的数据
Optional employee1 = list.stream().max(Comparator.comparing(Employee::getSalary));
System.out.println(employee1);
//获取集合中最大的工资值
OptionalLong dd = list.stream().mapToLong(Employee::getSalary).max();
System.out.println(dd);
//OptionalLong 转 long
long gg = dd.getAsLong();
System.out.println(gg);
//Optional 转 Employee
Employee e = employee1.get();
System.out.println(e);
}
-
lambda四大核心函数式接口
public static void main(String[] args) {
/**
* Consumer:消费型接口
* void accept(T t)
*
* Supplier:供给型接口
* T get()
*
* Function:函数型接口
* R apply(T t)
*
* Predicate: 断言型接口
* boolean test()
*/
String str = "开始 倾国倾城 /t/t/t/end";
//对字符串进行大写转化
String c = handler(str, e -> e.toUpperCase());
System.out.println(c);
}
public static void consumer(double money, Consumer con) {
con.accept(money);
}
public static String handler(String str, Function function) {
return function.apply(str);
}