学习lambda表达式总结
因为最近开发涉及到大量的集合数据处理,就开始研究lambda表达式使用,看了《Java8函数式编程》,同时研究了不少博客,总结了一些基础的用法,写一篇博客,为以后的使用提供便利。
下面介绍一下测试实体类:
import java.math.BigDecimal; /**
* @功能:【UserInfo 用户测试类】
* @作者:代守诚
* @日期:2018/11/14
* @时间:14:38
*/
public class UserInfo { private Integer age; private String name; private String address; private BigDecimal money; public BigDecimal getMoney() {
return money;
} public void setMoney(BigDecimal money) {
this.money = money;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "年龄:" + age + ", 名称:" + name + ", 地址:" + address;
} @Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof UserInfo)) {
return false;
} UserInfo userInfo = (UserInfo)obj;
return userInfo.getName().equals(name) && userInfo.getAddress().equals(address) && userInfo.getAge().equals(age);
} @Override
public int hashCode() {
int result = 17;
result += 31 * address.hashCode();
result += 31 * age.hashCode();
result += 31 * name.hashCode();
result += 31 * money.hashCode();
return result;
}
}
下面为大家介绍各种测试,我使用的Junit测试:
import com.hlxj.lambda.test.User.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert; import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream; import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals; /**
* @功能:【LambdaTest 】
* @作者:代守诚
* @日期:2018/11/14
* @时间:14:56
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class LambdaTest { /**
* 并集去重
*/
@Test
public void testDistinct() {
List<UserInfo> userInfoList = getUserInfo();
List<UserInfo> infos = getUser(); userInfoList.addAll(infos); //需要重写equals和hashCode方法
List<UserInfo> userInfos = userInfoList.stream().distinct().collect(toList()); userInfos.forEach(System.out :: println); //一行解决
/**
* 这个list的streamAPI的聚合操作collect可以让我们只关注结果,而collect方法里的collectingAndThen又是属于
* java.util.stream.Collector,collectingAndThen操作的解释是:先执行前面的操作,然后执行第二部操作后输出结果,
* 这里我们执行的第一步操作就是Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))),
* 第二步就是将他输出为一个新的ArrayList。
*
* 第一步操作里又是用到Collectors接口,这次用的是toCollection方法,就是将方法里的函数或者参数转化为一个collection集合,这里,
* 我们是将Comparator.comparing(Person::getName)转化为一个collection,这个collection是一个TreeSet。也就是有序的。因为
* 我们需要去掉重复的值,这个set可以做到,而我们又要保持转化出来的collection依旧有序,所以使用的是一个TreeSet。
*
* Comparator.comparing(Person::getName)这里呢,又用到了java.util.Comparator接口,这个接口倒是挺常用的。使用的是他的comparing方法,
* 也就是比较参数的值是否相同,里面用到的是java8的新特性lambda表达式, :: 其实和.没太大区别,举个例子,最常用的System.out.println() 可以
* 表达为System.out::println,可以达到一样的效果
*/
// List<UserInfo> distinct = userInfoList.stream().collect(
// Collectors.collectingAndThen(Collectors.toCollection(() ->new TreeSet<>(Comparator.comparing(UserInfo::getName))), ArrayList::new));
//
// distinct.forEach(System.out::println);
} /**
* 测试reduce
*/
@Test
public void testReduce() {
int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
System.out.println(count);
} /**
* 测试Stream,flatMap,filter等
*/
@Test
public void testUserInfo() {
List<UserInfo> userInfoList = getUserInfo();
List<UserInfo> infos = getUser(); //寻找集合大于年龄大于15的用户集合
List<UserInfo> u = Stream.of(userInfoList, infos).flatMap(userInfos -> userInfos.stream()).
filter(userInfo -> userInfo.getAge() > 15).collect(toList()); u.forEach(System.out::println); //寻找集合大于年龄大于15的姓名集合
List<String> us = Stream.of(userInfoList, infos).flatMap(userInfos -> userInfos.stream()).
filter(userInfo -> userInfo.getAge() > 15).map(userInfo -> userInfo.getName()).collect(toList());
us.forEach(System.out::println); //并集去重
List<UserInfo> distinct = Stream.of(userInfoList, infos).flatMap(userInfos -> userInfos.stream()).
collect(Collectors.collectingAndThen(Collectors.toCollection(() ->new TreeSet<>(Comparator.comparing(UserInfo::getName))), ArrayList::new)); distinct.forEach(System.out::println);
} /**
* 测试输出一个String
*/
@Test
public void testSttringBuilder() {
List<UserInfo> infos = getUser(); String result = infos.stream().map(UserInfo::getName).collect(Collectors.joining(", ", "[", "]"));
System.out.println(result);
} /**
* 测试下游收集器
*/
@Test
public void testMapping() {
List<UserInfo> userInfoList = getUserInfo(); Map<String, List<String>> map = userInfoList.stream().collect(groupingBy(UserInfo::getName, mapping(UserInfo::getAddress, toList()))); map.forEach((k, v) ->{
v.forEach(System.out::println);
});
} /**
* 测试并行化统计
*/
@Test
public void testCount() {
List<UserInfo> userInfoList = getUserInfo(); //整数求和
int count = userInfoList.parallelStream().mapToInt(UserInfo::getAge).sum();
System.out.println(count); //整数求最大
OptionalInt opMax = userInfoList.stream().mapToInt(UserInfo::getAge).max();
System.out.println(opMax.getAsInt()); //整数求最小
OptionalInt opMin = userInfoList.stream().mapToInt(UserInfo::getAge).min();
System.out.println(opMin.getAsInt()); //整数求平均值
OptionalDouble opAverage = userInfoList.stream().mapToInt(UserInfo::getAge).average();
System.out.println(opAverage.getAsDouble()); //计算金额
BigDecimal bigDecimal = userInfoList.stream().map(UserInfo::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(bigDecimal.doubleValue());
} /**
* 测试将集合中一个字段转成大写
*/
@Test
public void testStringUpperCase() {
List<UserInfo> userInfoList = getUserInfo(); //测试将字段转成大写
List<String> list = userInfoList.stream().map(userInfo -> userInfo.getName().toUpperCase()).collect(toList());
//测试将字段转成大写
List<String> stringList = userInfoList.stream().map(userInfo -> userInfo.getName().toUpperCase()).collect(Collectors.<String>toList());
//测试将字段首字母转成大写
List<String> strings = userInfoList.stream().map(userInfo -> userInfo.getName()).collect(Collectors.<String>toList());
List<String > sList = strings.stream().map(value ->{
char firstChar = Character.toUpperCase(value.charAt(0));
return firstChar + value.substring(1);
}).collect(Collectors.<String>toList()); list.forEach(System.out::println);
stringList.forEach(System.out::println);
sList.forEach(System.out::println);
} /**
* 测试集合list转换
*/
@Test
public void testList() {
List<UserInfo> userInfoList = getUserInfo();
List<UserInfo> infos = getUser(); //将list转成map
Map<Integer, UserInfo> map = userInfoList.stream().collect(Collectors.toMap(UserInfo::getAge, (k) -> k));
System.out.println(map);
map.forEach((k1, k2) -> {
System.out.println(k2);
});
Map<Integer, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getAge, userInfo -> userInfo,(k1, k2) -> k1));
userInfoMap.forEach((k1, k2) -> {
System.out.println(k2);
}); //根据某个属性进行分组计数
Map<String, Long> countMap = infos.stream().collect(Collectors.groupingBy(UserInfo::getName, Collectors.counting()));
System.out.println(countMap); //根据整个实体对象分组计数,当期为String时常用
Map<UserInfo, Long> userMap = infos.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(userMap); //根据分组的key值对结果进行排序、放进另一个map中并输出
Map<Integer, UserInfo> longUserInfoMap = new HashMap<>();
userInfoMap.entrySet().stream().sorted(Map.Entry.<Integer, UserInfo>comparingByKey().reversed()).
forEachOrdered(x ->longUserInfoMap.put(x.getKey(), x.getValue()));
System.out.println(longUserInfoMap); //分组并对其中一个属性求和
Map<String, Integer> uMap = userInfoList.stream().collect(Collectors.groupingBy(UserInfo::getName, Collectors.summingInt(UserInfo::getAge)));
System.out.println(uMap); //多个集合分组并对其中一个属性求和
Map<String, Integer> userM = Stream.of(userInfoList, infos).flatMap(userInfos -> userInfos.stream()).collect(Collectors.groupingBy(UserInfo::getName, Collectors.summingInt(UserInfo::getAge)));
System.out.println(userM);
} /**
* 测试交集、差集、并集
*/
@Test
public void testCollection() {
List<UserInfo> userInfoList = getUserInfo();
List<UserInfo> infos = getUser(); //测试交集
List<UserInfo> list = userInfoList.stream().filter(userInfo -> infos.contains(userInfo)).collect(toList());
list.forEach(System.out::println); //测试差集
List<UserInfo> infoList = userInfoList.stream().filter(userInfo -> !infos.contains(userInfo)).collect(toList());
infoList.forEach(System.out::println); //测试并集
List<UserInfo> userInfos = Stream.of(userInfoList, infos).flatMap(users -> users.stream()).collect(toList());
userInfos.forEach(System.out::println); } private List<UserInfo> getUserInfo() {
List<UserInfo> userInfoList = new ArrayList<>(); for(int count = 1; count < 10; count ++) {
UserInfo userInfo = new UserInfo();
userInfo.setAge(11 + count);
userInfo.setAddress("峡谷" + count);
userInfo.setName("dai" + count);
userInfo.setMoney(new BigDecimal(1.11));
userInfoList.add(userInfo);
}
return userInfoList;
} private List<UserInfo> getUser() {
List<UserInfo> userInfoList = new ArrayList<>(); for(int count = 5; count < 10; count ++) {
UserInfo userInfo = new UserInfo();
userInfo.setAge(11 + count);
userInfo.setAddress("峡谷" + count);
userInfo.setName("dai" + count);
userInfo.setMoney(new BigDecimal(1.11));
userInfoList.add(userInfo);
} return userInfoList;
}
}
上面是整体的代码,每一个方法对应的意义都是有注释,大家可以针对自己需要的寻找合适方法完成自己的代码需求。
具体的代码也可以上GitHub上面下载,代码地址:https://github.com/shouchengdai/lambda/tree/dev。
学习lambda表达式总结的更多相关文章
- Java8 新特性学习 Lambda表达式 和 Stream 用法案例
Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...
- Python学习-lambda表达式
lambda只是一个表达式,函数体比def简单很多.lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去.lambda表达式是起到一个函数速写的作用.允许在 ...
- 释放Android的函数式能量(I):Kotlin语言的Lambda表达式
原文标题:Unleash functional power on Android (I): Kotlin lambdas 原文链接:http://antonioleiva.com/operator-o ...
- C# Lambda表达式详解,及Lambda表达式树的创建
最近由于项目需要,刚刚学完了Action委托和Func<T>委托,发现学完了委托就必须学习lambda表达式,委托和Lambda表达式联合起来,才能充分的体现委托的便利.才能使代码更加简介 ...
- LinQ—Lambda表达式
概述 本篇博客主要解说lambda表达式,在这里将它的来龙去脉,主要是从托付,匿名函数这些方面过度讲的,当然,在讲托付和匿名函数的时候,主要是从Lambda的角度出发讲的,可能它们还具有其他的一些作用 ...
- 深入浅出 Java 8 Lambda 表达式
摘要:此篇文章主要介绍 Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等.本文系 OneAPM 工程师编译整理. Java 是一流的面向对象语言,除了部分简 ...
- java8 Lambda表达式的新手上车指南(1)
背景 java9的一再推迟发布,似乎让我们恍然想起离发布java8已经过去了三年之久,java8应该算的上java语言在历代版本中变化最大的一个版本了,最大的新特性应该算得上是增加了lambda表达式 ...
- java8 Lambda表达式的新手上车指南(1)--基础语法和函数式接口
背景 java9的一再推迟发布,似乎让我们恍然想起离发布java8已经过去了三年之久,java8应该算的上java语言在历代版本中变化最大的一个版本了,最大的新特性应该算得上是增加了lambda表达式 ...
- Java8新特性-Lambda表达式是什么?
目录 前言 匿名内部类 函数式接口 和 Lambda表达式语法 实现函数式接口并使用Lambda表达式: 所以Lambda表达式是什么? 实战应用 总结 前言 Java8新特性-Lambda表达式,好 ...
随机推荐
- 帆软报表(finereport)单元格中各颜色标识的含义
帆软报表(finereport)单元格中,可根据单元格角标的颜色判断单元格进行的操作 过滤:单元格左下角黄色三角形 条件属性:单元格左上角红色三角形. 控件:单元格右侧中间的各种矩形. 左父格:单 ...
- Vue项目中使用webpack配置了别名,引入的时候报错
chainWebpack(config) { config.resolve.alias .set('@', resolve('src')) .set('assets', resolve('src/as ...
- 创建免费的证书,实现网站HTTPS
使用Certbot来实现HTTPS,这边也就考虑采用Cerbot来实现下 配置Certbot 证书 Certbot 的官方网站是 https://certbot.eff.org/ ,打开官网选择的 w ...
- 重写 console.log()
/*重写console.log*/ console.log = (function(mFun){ return function(str){ mFun.call(console,'hello! ' + ...
- vmware ubuntu硬盘空间不够用,空间扩展
我从来没有想过我的虚拟机内存会不够用,毕竟已经20G了,可是最近学习python,装了些学习有关的软件, 期末做libvirt管理实验,存了两个镜像,就变成这样了,所以,我就像了要扩展硬盘空间,在网上 ...
- Redis数据结构之ziplist
本文及后续文章,Redis版本均是v3.2.8 本篇文章我们来分析下一种特殊编码的双向链表-ziplist(压缩列表),这种数据结构的功能是将一系列数据与其编码信息存储在一块连续的内存区域,这块内存物 ...
- 学习django就看这本书了!django book 2.0中文版
所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/29/ 来源:python黑洞网 dj ...
- 自定义InputFormat和OutputFormat案例
一.自定义InputFormat InputFormat是输入流,在前面的例子中使用的是文件输入输出流FileInputFormat和FileOutputFormat,而FileInputFormat ...
- 调试webpack配置文件
webpack运行在nodejs上,调试webpack就相当于调试nodejs程序.下面介绍一种通用的办法. 1.配置package.json,加一个debug. { 'scripts': { 'de ...
- XIII Open Grodno SU Championship
A. Alice in the Wonderland 按题意模拟. #include<stdio.h> #include<iostream> #include<strin ...