一.关于Lambda表达式
forEach and Map
1、通常这样遍历一个Map
1 2 3 4 5 6 7 8 9 10 11 | Map<String, Integer> items = new HashMap<>(); items.put( "A" , 10 ); items.put( "B" , 20 ); items.put( "C" , 30 ); items.put( "D" , 40 ); items.put( "E" , 50 ); items.put( "F" , 60 ); for (Map.Entry<String, Integer> entry : items.entrySet()) { System.out.println( "Item : " + entry.getKey() + " Count : " + entry.getValue()); } |
2、在java8中你可以使用 foreach + 拉姆达表达式遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Map<String, Integer> items = new HashMap<>(); items.put( "A" , 10 ); items.put( "B" , 20 ); items.put( "C" , 30 ); items.put( "D" , 40 ); items.put( "E" , 50 ); items.put( "F" , 60 ); items.forEach((k,v)->System.out.println( "Item : " + k + " Count : " + v)); items.forEach((k,v)->{ System.out.println( "Item : " + k + " Count : " + v); if ( "E" .equals(k)){ System.out.println( "Hello E" ); } }); |
forEach and List
1、通常这样遍历一个List
1 2 3 4 5 6 7 8 9 10 | List<String> items = new ArrayList<>(); items.add( "A" ); items.add( "B" ); items.add( "C" ); items.add( "D" ); items.add( "E" ); for (String item : items){ System.out.println(item); } |
2、在java8中你可以使用 foreach + 拉姆达表达式 或者 method reference(方法引用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | List<String> items = new ArrayList<>(); items.add( "A" ); items.add( "B" ); items.add( "C" ); items.add( "D" ); items.add( "E" ); //lambda //Output : A,B,C,D,E items.forEach(item->System.out.println(item)); //Output : C items.forEach(item->{ if ( "C" .equals(item)){ System.out.println(item); } }); //method reference //Output : A,B,C,D,E items.forEach(System.out::println); //Stream and filter //Output : B items.stream() .filter(s->s.contains( "B" )) .forEach(System.out::println); |
二、关于Collections分组
1、 跟据某个属性分组OfficeId:
Map<String, List<IncomeSumPojo>> collect = list.stream().collect(Collectors.groupingBy(IncomeSumPojo::getOfficeId));
2、根据某个属性分组OfficeId,汇总某个属性Money:
Map<String, Double> collect = list.stream().collect(Collectors.groupingBy(IncomeSumPojo::getOfficeId,Collectors.summingDouble(IncomeSumPojo::getMoney)));
3、根据某个属性添加条件过滤数据:
list = list.stream().filter(u -> !u.getAmount().equals("0.00")).collect(Collectors.toList());
4、判断一组对象里面有没有属性值是某个值:
List<Menu> menuList = UserUtils.getMenuList();
boolean add = menuList.stream().anyMatch(m -> "plan:ctPlan:add".equals(m.getPermission()));
5、取出一组对象的某个属性组成一个新集合:
List<String> tableNames=list.stream().map(User::getMessage).collect(Collectors.toList());
6、list去重复:
list = list.stream().distinct().collect(Collectors.toList());
例子:
先准备一点数据:
public class User { private Integer id; private String type; private String name; private String date; // omit setter and getter } User user1 = new User(1,"张三","小学", "2017-03"); User user2 = new User(2,"李四","小学", "2017-03"); User user3 = new User(3,"王五","初中", "2017-02"); User user4 = new User(4,"马六","高中", "2017-04"); User user5= new User(5,"jack","大学", "2017-04"); List<User > list = new ArrayList<>(); list.add(user1 ); list.add(user2 ); list.add(user3 ); list.add(user4 ); list.add(user5);
现在想对用户列表根据日期进行分组,得到一个map: date -> List<User>。
Java7以前也许是这样的:
List<User> userList = Arrays.asList(user1, user2, user3, user5, user4); Map<String, List<User>> groupUserMap = Maps.newHashMap(); for (User user : userList) { String month = user.getDate(); if (groupUserMap.containsKey(month)) { groupUserMap.get(month).add(user); } else { groupUserMap.put(month, Lists.newArrayList(user)); } }
有了Java8之后,一切很简单了:
Map<String, List<User>> groupUserMapOne =userList.stream().collect(Collectors.groupingBy(User::getDate));
如果想分组的List里是User的name呢:
Map<String, List<String>> userByDate = Stream.of(user1, user2, user3, user5, user4).collect(Collectors.groupingBy(User::getDate,
Collectors.mapping(User::getName, Collectors.toList())));
System.out.println(userByDate);
三、关于Stream
1、collect(Collectors.toList())
将流转换为list。还有toSet(),toMap()等。及早求值。
public class TestCase { public static void main(String[] args) { List<Student> studentList = Stream.of(new Student("路飞", 22, 175), new Student("红发", 40, 180), new Student("白胡子", 50, 185)).collect(Collectors.toList()); System.out.println(studentList); } } //输出结果 //[Student{name='路飞', age=22, stature=175, specialities=null}, //Student{name='红发', age=40, stature=180, specialities=null}, //Student{name='白胡子', age=50, stature=185, specialities=null}]
2、filter
顾名思义,起过滤筛选的作用。内部就是Predicate接口。惰性求值。
比如我们筛选出出身高小于180的同学。
public class TestCase { public static void main(String[] args) { List<Student> students = new ArrayList<>(3); students.add(new Student("路飞", 22, 175)); students.add(new Student("红发", 40, 180)); students.add(new Student("白胡子", 50, 185)); List<Student> list = students.stream() .filter(stu -> stu.getStature() < 180) .collect(Collectors.toList()); System.out.println(list); } } //输出结果 //[Student{name='路飞', age=22, stature=175, specialities=null}]
3、map
转换功能,内部就是Function接口。惰性求值。
public class TestCase { public static void main(String[] args) { List<Student> students = new ArrayList<>(3); students.add(new Student("路飞", 22, 175)); students.add(new Student("红发", 40, 180)); students.add(new Student("白胡子", 50, 185)); List<String> names = students.stream().map(student -> student.getName()) .collect(Collectors.toList()); System.out.println(names); } } //输出结果 //[路飞, 红发, 白胡子]
4、flatMap
将多个Stream合并为一个Stream。惰性求值。
public class TestCase { public static void main(String[] args) { List<Student> students = new ArrayList<>(3); students.add(new Student("路飞", 22, 175)); students.add(new Student("红发", 40, 180)); students.add(new Student("白胡子", 50, 185)); List<Student> studentList = Stream.of(students, asList(new Student("艾斯", 25, 183), new Student("雷利", 48, 176))) .flatMap(students1 -> students1.stream()).collect(Collectors.toList()); System.out.println(studentList); } } //输出结果 //[Student{name='路飞', age=22, stature=175, specialities=null}, //Student{name='红发', age=40, stature=180, specialities=null}, //Student{name='白胡子', age=50, stature=185, specialities=null}, //Student{name='艾斯', age=25, stature=183, specialities=null}, //Student{name='雷利', age=48, stature=176, specialities=null}]
调用Stream.of的静态方法将两个list转换为Stream,再通过flatMap将两个流合并为一个。
5、max和min
我们经常会在集合中求最大或最小值,使用流就很方便。及早求值。
public class TestCase { public static void main(String[] args) { List<Student> students = new ArrayList<>(3); students.add(new Student("路飞", 22, 175)); students.add(new Student("红发", 40, 180)); students.add(new Student("白胡子", 50, 185)); Optional<Student> max = students.stream() .max(Comparator.comparing(stu -> stu.getAge())); Optional<Student> min = students.stream() .min(Comparator.comparing(stu -> stu.getAge())); //判断是否有值 if (max.isPresent()) { System.out.println(max.get()); } if (min.isPresent()) { System.out.println(min.get()); } } } //输出结果 //Student{name='白胡子', age=50, stature=185, specialities=null} //Student{name='路飞', age=22, stature=175, specialities=null}
max、min接收一个Comparator(例子中使用java8自带的静态函数,只需要传进需要比较值即可。)并且返回一个Optional对象,该对象是java8新增的类,专门为了防止null引发的空指针异常。
可以使用max.isPresent()判断是否有值;可以使用max.orElse(new Student()),当值为null时就使用给定值;也可以使用max.orElseGet(() -> new Student());这需要传入一个Supplier的lambda表达式。
6、 count
统计功能,一般都是结合filter使用,因为先筛选出我们需要的再统计即可。及早求值。
public class TestCase { public static void main(String[] args) { List<Student> students = new ArrayList<>(3); students.add(new Student("路飞", 22, 175)); students.add(new Student("红发", 40, 180)); students.add(new Student("白胡子", 50, 185)); long count = students.stream().filter(s1 -> s1.getAge() < 45).count(); System.out.println("年龄小于45岁的人数是:" + count); } } //输出结果 //年龄小于45岁的人数是:2
7、 reduce
reduce 操作可以实现从一组值中生成一个值。在上述例子中用到的 count 、 min 和 max 方法,因为常用而被纳入标准库中。事实上,这些方法都是 reduce 操作。及早求值。
public class TestCase { public static void main(String[] args) { Integer reduce = Stream.of(1, 2, 3, 4).reduce(0, (acc, x) -> acc+ x); System.out.println(reduce); } } //输出结果 //10
我们看得reduce接收了一个初始值为0的累加器,依次取出值与累加器相加,最后累加器的值就是最终的结果。
8、转换成值
收集器,一种通用的、从流生成复杂值的结构。只要将它传给 collect 方法,所有的流就都可以使用它了。标准类库已经提供了一些有用的收集器,以下示例代码中的收集器都是从 java.util.stream.Collectors 类中静态导入的。
public class CollectorsTest { public static void main(String[] args) { List<Student> students1 = new ArrayList<>(3); students1.add(new Student("路飞", 23, 175)); students1.add(new Student("红发", 40, 180)); students1.add(new Student("白胡子", 50, 185)); OutstandingClass ostClass1 = new OutstandingClass("一班", students1); //复制students1,并移除一个学生 List<Student> students2 = new ArrayList<>(students1); students2.remove(1); OutstandingClass ostClass2 = new OutstandingClass("二班", students2); //将ostClass1、ostClass2转换为Stream Stream<OutstandingClass> classStream = Stream.of(ostClass1, ostClass2); OutstandingClass outstandingClass = biggestGroup(classStream); System.out.println("人数最多的班级是:" + outstandingClass.getName()); System.out.println("一班平均年龄是:" + averageNumberOfStudent(students1)); } /** * 获取人数最多的班级 */ private static OutstandingClass biggestGroup(Stream<OutstandingClass> outstandingClasses) { return outstandingClasses.collect( maxBy(comparing(ostClass -> ostClass.getStudents().size()))) .orElseGet(OutstandingClass::new); } /** * 计算平均年龄 */ private static double averageNumberOfStudent(List<Student> students) { return students.stream().collect(averagingInt(Student::getAge)); } } //输出结果 //人数最多的班级是:一班 //一班平均年龄是:37.666666666666664
maxBy或者minBy就是求最大值与最小值。
9、转换成块
常用的流操作是将其分解成两个集合,Collectors.partitioningBy帮我们实现了,接收一个Predicate函数式接口。
将示例学生分为会唱歌与不会唱歌的两个集合。
public class PartitioningByTest { public static void main(String[] args) { //省略List<student> students的初始化 Map<Boolean, List<Student>> listMap = students.stream().collect( Collectors.partitioningBy(student -> student.getSpecialities(). contains(SpecialityEnum.SING))); } }
10、数据分组
数据分组是一种更自然的分割数据操作,与将数据分成 ture 和 false 两部分不同,可以使用任意值对数据分组。Collectors.groupingBy接收一个Function做转换。
例子:根据学生第一个特长进行分组。
public class GroupingByTest { public static void main(String[] args) { //省略List<student> students的初始化 Map<SpecialityEnum, List<Student>> listMap = students.stream().collect( Collectors.groupingBy(student -> student.getSpecialities().get(0))); } }
Collectors.groupingBy与SQL 中的 group by 操作是一样的。
11、字符串拼接
如果将所有学生的名字拼接起来,怎么做呢?通常只能创建一个StringBuilder,循环拼接。使用Stream,使用Collectors.joining()简单容易。
public class JoiningTest { public static void main(String[] args) { List<Student> students = new ArrayList<>(3); students.add(new Student("路飞", 22, 175)); students.add(new Student("红发", 40, 180)); students.add(new Student("白胡子", 50, 185)); String names = students.stream() .map(Student::getName).collect(Collectors.joining(",","[","]")); System.out.println(names); } } //输出结果 //[路飞,红发,白胡子]
joining接收三个参数,第一个是分界符,第二个是前缀符,第三个是结束符。也可以不传入参数Collectors.joining(),这样就是直接拼接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix