Lambda表达式
Lambda表达式
前言
日常开发中,我们很多时候需要用到
Java 8的Lambda表达式,它允许把函数作为一个方法的参数,让我们的代码更优雅、更简洁。所以整理了一波工作中,我常用的,有哪些Lambda表达式。看完一定会有帮助的。
1.list转map
工作中,我们经常遇到
list转map的案例。Collectors.toMap就可以把一个list数组转成一个Map。代码如下:
View Codepublic class Demo_List_To_Map { public static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); /** * list 转 map * 使用Collectors.toMap的时候,如果有可以重复会报错,所以需要加(k1, k2) -> k1 * (k1, k2) -> k1 表示,如果有重复的key,则保留第一个,舍弃第二个 */ Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo, (k1, k2) -> k1)); userInfoMap.values().forEach(a -> System.out.println(a.getUserName())); } @Data @AllArgsConstructor @NoArgsConstructor static class UserInfo { Long UserId; String UserName; Integer age; } } //运行结果 我的源码 程序员张三类似的,还有
Collectors.toList()、Collectors.toSet(),表示把对应的流转化为list或者Set。
2.filter()过滤
从数组集合中,过滤掉不符合条件的元素,留下符合条件的元素。
View Codepublic static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); /** * filter 过滤,留下超过18岁的用户 */ List<UserInfo> userInfoResultList = userInfoList.stream().filter(user -> user.getAge() > 18).collect(Collectors.toList()); userInfoResultList.forEach(a -> System.out.println(a.getUserName())); } //运行结果 我的源码 程序员张三
3.foreach遍历
foreach 遍历list,遍历map,真的很丝滑。
View Codepublic static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); /** * forEach 遍历集合List列表 */ userInfoList.forEach(System.out::println); HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("公众号", "我的源码"); hashMap.put("职业", "程序员张三"); hashMap.put("昵称", "打代码的张三"); /** * forEach 遍历集合Map */ hashMap.forEach((k, v) -> System.out.println(k + ":\t" + v)); } //运行结果 Demo.UserInfo(UserId=1, UserName=我的源码, age=18) Demo.UserInfo(UserId=2, UserName=程序员张三, age=27) Demo.UserInfo(UserId=2, UserName=打代码的张三, age=26) 职业: 程序员张三 公众号: 我的源码 昵称: 打代码的张三
4.groupingBy分组
View Codepublic static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); /** * Java8之前的实现 */ Map<Integer, List<UserInfo>> result = new HashMap<>(); for (UserInfo userInfo : userInfoList) { Integer age = userInfo.getAge(); List<UserInfo> userInfos = result.get(age); if (userInfos == null) { userInfos = new ArrayList<>(); result.put(age, userInfos); } userInfos.add(userInfo); } result.forEach((k, v) -> System.out.println(k + ":\t" + v)); /** * Java8的groupingBy分组器 */ Map<Integer, List<UserInfo>> resultLam = userInfoList.stream() .collect(Collectors.groupingBy(UserInfo::getAge)); resultLam.forEach((k, v) -> System.out.println(k + ":\t" + v)); } //运行结果 18: [Demo.UserInfo(UserId=1, UserName=我的源码, age=18)] 26: [Demo.UserInfo(UserId=2, UserName=打代码的张三, age=26)] 27: [Demo.UserInfo(UserId=2, UserName=程序员张三, age=27)] 18: [Demo.UserInfo(UserId=1, UserName=我的源码, age=18)] 26: [Demo.UserInfo(UserId=2, UserName=打代码的张三, age=26)] 27: [Demo.UserInfo(UserId=2, UserName=程序员张三, age=27)]
5.sorted+Comparator排序
工作中,排序的需求比较多,使用
sorted+Comparator排序,真的很香。
View Codepublic static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); /** * sorted + Comparator.comparing 排序列表, */ userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList()); userInfoList.forEach(a -> System.out.println(a.toString())); System.out.println("开始降序排序"); /** * 如果想降序排序,则可以使用加reversed() */ userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList()); userInfoList.forEach(a -> System.out.println(a.toString())); } //运行结果 Demo.UserInfo(UserId=1, UserName=我的源码, age=18) Demo.UserInfo(UserId=2, UserName=打代码的张三, age=26) Demo.UserInfo(UserId=2, UserName=程序员张三, age=27) 开始降序排序 Demo.UserInfo(UserId=2, UserName=程序员张三, age=27) Demo.UserInfo(UserId=2, UserName=打代码的张三, age=26) Demo.UserInfo(UserId=1, UserName=我的源码, age=18)
6.distinct去重
distinct可以去除重复的元素
View CodeList<String> list = Arrays.asList("A", "B", "F", "A", "C"); List<String> temp = list.stream().distinct().collect(Collectors.toList()); temp.forEach(System.out::println);
7.findFirst返回第一个
findFirst很多业务场景,我们只需要返回集合的第一个元素即可
View CodeList<String> list = Arrays.asList("A", "B", "F", "A", "C"); // ifPresent(): 一个容器对象,它可能包含也可能不包含非空值。 // 如果存在一个值,isPresent()将返回true, get()将返回值。 list.stream().findFirst().ifPresent(System.out::println);
8.anyMatch是否至少匹配一个元素
anyMatch检查流是否包含至少一个满足给定谓词的元素
View CodeStream<String> stream = Stream.of("A", "B", "C", "D"); boolean match = stream.anyMatch(s -> s.contains("C")); System.out.println(match); //输出 true
9.allMatch 匹配所有元素
allMatch检查流是否所有都满足给定谓词的元素
View CodeStream<String> stream = Stream.of("A", "B", "C", "D"); boolean match = stream.allMatch(s -> s.contains("C")); System.out.println(match); //输出 false
10.map转换
map方法可以帮我们做元素转换,比如一个元素所有字母转化为大写,又或者把获取一个元素对象的某个属性,demo如下:
View CodeList<String> list = Arrays.asList("jay", "tianluo"); //转化为大写 List<String> upperCaselist = list.stream().map(String::toUpperCase).collect(Collectors.toList()); upperCaselist.forEach(System.out::println);
11.Reduce
Reduce可以合并流的元素,并生成一个值
View Codeint sum = Stream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b); System.out.println(sum);
12.peek打印个日志
peek()方法是一个中间Stream操作,有时候我们可以使用peek来打印日志。
View Codepublic static void main(String[] args) { List<String> result = Stream.of("我的源码", "程序员张三", "打代码的张三") .filter(a -> a.contains("张三")) .peek(a -> System.out.println("关注公众号:" + a)).collect(Collectors.toList()); System.out.println(result); } //运行结果 关注公众号:程序员张三 关注公众号:打代码的张三 [程序员张三, 打代码的张三]
13.Max&Min最大最小
使用lambda流求最大,最小值,非常方便
View Codepublic static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); /** 最大值 */ userInfoList.stream().max(Comparator.comparing(UserInfo::getAge)) .ifPresent(userInfo -> System.out.println("max age user:" + userInfo)); /** 最小值 */ userInfoList.stream().min(Comparator.comparing(UserInfo::getAge)) .ifPresent(userInfo -> System.out.println("min age user:" + userInfo)); } //运行结果 max age user:Demo.UserInfo(UserId=2, UserName=程序员张三, age=27) min age user:Demo.UserInfo(UserId=1, UserName=我的源码, age=18)
14.count统计
一般
count()表示获取流数据元素总数
View Codepublic static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "我的源码", 18)); userInfoList.add(new UserInfo(2L, "程序员张三", 27)); userInfoList.add(new UserInfo(2L, "打代码的张三", 26)); long count = userInfoList.stream().filter(user -> user.getAge() > 18).count(); System.out.println("大于18岁的用户:" + count); } //运行结果 大于18岁的用户:2
15.常用函数式接口
其实lambda离不开函数式接口,我们来看下JDK8常用的几个函数式接口:
Function<T, R>(转换型): 接受一个输入参数,返回一个结果
Consumer<T>(消费型): 接收一个输入参数,并且无返回操作
Predicate<T>(判断型): 接收一个输入参数,并且返回布尔值结果
Supplier<T>(供给型): 无参数,返回结果
Function<T, R>是一个功能转换型的接口,可以把将一种类型的数据转化为另外一种类型的数据
View Codepublic static void main(String[] args) { /** * Function <T,R> => 其中占位符T(type)代表入参类型,R(return)代表返回类型 */ /** * 案例1:获取每个字符串的长度,并且返回 */ Function<String, Integer> function = String::length; Stream<String> stream = Stream.of("我的源码", "程序员张三", "打代码的张三"); Stream<Integer> resultStream = stream.map(function); resultStream.forEach(System.out::println); /** * 案例2:获取每个字符串的长度,并且返回 * * apply方法是将入参带入方法中 * compose()括号中的内容在调用函数time2之前运行 4*4=16 16*2 =32 * andThen()括号中的内容在调用函数time2结束之后运行 4*2 =8 8*8=64 * identity() 方法直接返回传入的参数 */ Function<Integer, Integer> times2 = e -> e * 2; Function<Integer, Integer> squared = e -> e * e; //将参数代入(10*2=20) Integer apply2 = times2.apply(10); //在times2函数执行之前先执行squared函数 (先运行 4*4=16 再 16*2=32) Integer apply = times2.compose(squared).apply(4); //在times2执行之后,再执行squared函数 (先运行4*2=8 再8*8=64) Integer apply1 = times2.andThen(squared).apply(4); //直接返回传入的参数 Function<String, String> fun1 = Function.identity(); String str1 = fun1.apply("helloWorld"); System.out.println("apply == " + apply); //apply == 32 System.out.println("apply1 == " + apply1); //apply1 == 64 System.out.println("apply2 == " + apply2); //apply2 == 20 }
Consumer<T>是一个消费性接口,通过传入参数,并且无返回的操作
View Codepublic static void main(String[] args) { /** * Consumer<T> => 意思就是他接收一个参数,没有返回,全靠副作用来做事情,可以当做void来看。 */ /** * 案例1:获取每个字符串的长度,并且返回 */ //获取每个字符串的长度,并且返回 Consumer<String> consumer = System.out::println; Stream<String> stream = Stream.of("我的源码", "程序员张三", "打代码的张三"); stream.forEach(consumer); /** * 案例2:获取每个字符串的长度,并且返回 * * accept()输入参数、执行方法 * andThen()括号中的内容在调用函数cons结束之后运行 */ Consumer<Integer> cons = (x) -> { System.out.println("第一个Consumer: " + (++x)); // 51 }; Consumer<Integer> cons1 = (x) -> { System.out.println("第二个Consumer: " + (--x)); // 49 }; cons.andThen(cons1).accept(50); } Predicate<T>是一个判断型接口,并且返回布尔值结果 private void testPredicate() { //获取每个字符串的长度,并且返回 Predicate<Integer> predicate = a -> a > 18; UserInfo userInfo = new UserInfo(2L, "程序员芋艿", 27); System.out.println(predicate.test(userInfo.getAge())); }
View Codeprivate void testPredicate() { //获取每个字符串的长度,并且返回 Predicate<Integer> predicate = a -> a > 18; UserInfo userInfo = new UserInfo(2L, "程序员芋艿", 27); System.out.println(predicate.test(userInfo.getAge())); }
Supplier<T>是一个供给型接口,无参数,有返回结果
View Codeprivate void testSupplier() { Supplier<Integer> supplier = () -> Integer.valueOf("666"); System.out.println(supplier.get()); }

Java Lambda表达式

浙公网安备 33010602011771号