网站核验为个人实际是公司,怎么降低wordpress版本,织梦仿站时怎么取俩个网站的页面整合,外贸找客户平台Java 中 Stream 流的使用详解
什么是 Stream#xff1f;
Stream 是 Java 8 引入的一种全新的操作集合的方式。它支持通过声明性方式对集合进行复杂的数据操作#xff08;如过滤、排序、聚合等#xff09;#xff0c;避免使用大量的 for 循环#xff0c;提高代码的可读性…Java 中 Stream 流的使用详解
什么是 Stream
Stream 是 Java 8 引入的一种全新的操作集合的方式。它支持通过声明性方式对集合进行复杂的数据操作如过滤、排序、聚合等避免使用大量的 for 循环提高代码的可读性和简洁性。
Stream 具有以下特点
惰性执行流操作是延迟的只有在需要时才会执行。链式操作流的操作可以通过方法链的形式串联。不可变性Stream 本身不会修改原始数据结构。 Stream 的核心操作
Stream 流的操作分为三类
创建流通过集合、数组或生成器创建流。中间操作对数据流进行加工如 filter、map、sorted 等。终端操作触发流的执行如 collect、forEach、reduce 等。 创建 Stream
1. 从集合创建
ListString list List.of(Java, Python, C);
StreamString stream list.stream();2. 从数组创建
String[] array {Apple, Banana, Orange};
StreamString stream Arrays.stream(array);3. 使用生成器创建
StreamInteger infiniteStream Stream.iterate(0, n - n 2);
infiniteStream.limit(5).forEach(System.out::println); // 输出0, 2, 4, 6, 8中间操作
1. filter过滤数据
ListInteger numbers List.of(1, 2, 3, 4, 5, 6);
ListInteger evenNumbers numbers.stream().filter(n - n % 2 0).collect(Collectors.toList());
System.out.println(evenNumbers); // 输出[2, 4, 6]2. map映射转换
ListString names List.of(Alice, Bob, Charlie);
ListInteger nameLengths names.stream().map(String::length).collect(Collectors.toList());
System.out.println(nameLengths); // 输出[5, 3, 7]3. sorted排序
ListString fruits List.of(Banana, Apple, Orange);
ListString sortedFruits fruits.stream().sorted().collect(Collectors.toList());
System.out.println(sortedFruits); // 输出[Apple, Banana, Orange]4. distinct去重
ListInteger numbers List.of(1, 2, 2, 3, 4, 4, 5);
ListInteger uniqueNumbers numbers.stream().distinct().collect(Collectors.toList());
System.out.println(uniqueNumbers); // 输出[1, 2, 3, 4, 5]5. limit 和 skip截取和跳过
ListInteger numbers List.of(1, 2, 3, 4, 5, 6);
ListInteger limitedNumbers numbers.stream().limit(3).collect(Collectors.toList());
System.out.println(limitedNumbers); // 输出[1, 2, 3]ListInteger skippedNumbers numbers.stream().skip(3).collect(Collectors.toList());
System.out.println(skippedNumbers); // 输出[4, 5, 6]终端操作
1. collect收集结果
ListInteger numbers List.of(1, 2, 3, 4, 5);
SetInteger numberSet numbers.stream().collect(Collectors.toSet());
System.out.println(numberSet); // 输出[1, 2, 3, 4, 5]2. forEach遍历
ListString names List.of(Alice, Bob, Charlie);
names.stream().forEach(System.out::println);3. reduce聚合
ListInteger numbers List.of(1, 2, 3, 4, 5);
int sum numbers.stream().reduce(0, Integer::sum);
System.out.println(sum); // 输出154. count计数
ListString items List.of(Apple, Banana, Orange);
long count items.stream().filter(item - item.startsWith(A)).count();
System.out.println(count); // 输出15. findFirst 和 findAny查找元素
ListInteger numbers List.of(1, 2, 3, 4, 5);
OptionalInteger firstEven numbers.stream().filter(n - n % 2 0).findFirst();
firstEven.ifPresent(System.out::println); // 输出2案例复杂数据处理
数据准备
class Employee {String name;String department;double salary;Employee(String name, String department, double salary) {this.name name;this.department department;this.salary salary;}
}ListEmployee employees List.of(new Employee(Alice, HR, 5000),new Employee(Bob, IT, 7000),new Employee(Charlie, IT, 6000),new Employee(David, Finance, 8000)
);1. 查找 IT 部门的员工姓名
ListString itEmployees employees.stream().filter(emp - IT.equals(emp.department)).map(emp - emp.name).collect(Collectors.toList());
System.out.println(itEmployees); // 输出[Bob, Charlie]2. 按部门分组员工
MapString, ListEmployee groupedByDept employees.stream().collect(Collectors.groupingBy(emp - emp.department));
System.out.println(groupedByDept);3. 计算所有员工的平均工资
double averageSalary employees.stream().mapToDouble(emp - emp.salary).average().orElse(0);
System.out.println(averageSalary); // 输出6500.0注意事项
避免修改流中的元素Stream 是不可变的避免在流中修改元素。流的惰性求值中间操作只有在终端操作时才会执行。不要重复消费流流一旦操作完成就不能再次使用。 文章转载自: http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.blbys.cn.gov.cn.blbys.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.cjmmt.cn.gov.cn.cjmmt.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.wqpm.cn.gov.cn.wqpm.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn