stream流的一些常用用法
@AllArgsConstructor @NoArgsConstructor @Data public class Employee { private Long id; private String name; // 姓 private String city; // 城市 private Integer sales; // 销售额 private boolean status; // 销售额 }
常用方法 @Slf4j
public class StreamTest { static List<Employee> list = Arrays.asList( new Employee(1l,"李念", "西安", 800,true), new Employee(2l,"李四", "西安", 700,false), new Employee(3l,"王五", "西安", 400,false), new Employee(4l,"李东", "武汉", 530,true), new Employee(5l,"赵乐", "武汉", 350,false), new Employee(6l,"王卓", "武汉", 120,false), new Employee(7l,"皇子", "成都", 550,true), new Employee(1l,"李念", "成都", 300,true), new Employee(9l,"梁博", "成都", 450,false)); //编辑集合并统一给状态赋值 public static void testForEach() { list.stream().forEach(s -> s.setStatus(true) ); log.info("ForEach输出:数据:{},大小:{}", list.toArray(), list.size()); } //过滤出销售额大于400的员工 public static void testFilter() { List<Employee> listfilter = list.stream().filter(s->s.getSales() > 400).sorted(Comparator.comparing(Employee::getSales)).collect(Collectors.toList()); log.info("Filter输出:数据:{},大小:{}", listfilter, listfilter.size()); } //将员工名称转成集合并去掉前两个 public static List<String> testMap() { List<String> listMap = list.stream().map(Employee::getName).skip(2).collect(Collectors.toList()); log.info("Map输出:数据:{},大小:{}", listMap, listMap.size()); return listMap; } //过滤出状态是true 的前7组数据 public static void testFilterB() { List<Employee> listfilterB = list.stream().filter(Employee::isStatus).limit(7).collect(Collectors.toList()); log.info("FilterB输出:数据:{},大小:{}", listfilterB, listfilterB.size()); } //按城市分组,key为城市名称 public static void testGroupBy1(){ Map<String,List<Employee>> map = list.stream().collect(Collectors.groupingBy(Employee::getCity)); log.info("testGroupBy输出:数据:{},大小:{}", map, map.size()); log.info("======================================="); map.forEach((key,val)->{ System.out.println("城市:"+key+" ---销售额: "+val); }); } //分组计算每个城市总的销售额 public static void testGroupBy2(){ Map<String, Integer> map = list.stream(). collect(Collectors.groupingBy(Employee::getCity, Collectors.summingInt(Employee::getSales))); log.info("testGroupBy输出:数据:{},大小:{}", map, map.size()); map.forEach((key,value)->{ System.out.println("城市:"+key+" ---销售额: "+ value); }); } //分组计算每个城市的最高销售额 public static void testGroupBy3(){ Map<String, Employee> map = list.stream().collect(Collectors.groupingBy(Employee::getCity, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Employee::getSales)), Optional::get))); map.forEach((key,val)->{ System.out.println("城市:"+key+" 销售额最大员工:"+val); }); } //去重,选择最后一个 public static void testMap4(){ Map<Long, Employee> map = list.stream().collect(Collectors.toMap(Employee::getId,o->o,(k1,k2)->k2)); map.forEach((key,val)->{ System.out.println("城市:"+key+" 销售额最大员工:"+val); }); }
//获取最大值、最小值 public static void testReduce() { List<Integer> list1 = Arrays.asList(1,100,200); int sum = list1.stream().reduce(0,Integer::sum); int max = list1.stream().reduce(0,Integer::max); int min = list1.stream().reduce(0,Integer::min); log.info("FilterB sum输出:" + sum ); log.info("FilterB max输出:" + max ); log.info("FilterB min输出:" + min ); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构