上海网站建设的网,做动态图网站违法吗,做网站要先买域名吗,购物网站开发介绍Stream流 把数据放进stream流水线#xff0c;对数据进行一系列操作#xff08;中间方法#xff09;#xff0c;最后封装#xff08;终结方法#xff09;。 Stream.of()允许传入任何参数 常见中间方法 可以对数据进行链式#xff08;流水线#xff09;操作#xff0c;但…Stream流 把数据放进stream流水线对数据进行一系列操作中间方法最后封装终结方法。 Stream.of()允许传入任何参数 常见中间方法 可以对数据进行链式流水线操作但中间方法返回的都是stream对象 Stream.of(1, 2, 3, 4, 5).filter(n - n % 2 0) // 只保留偶数.forEach(System.out::println);//打印Stream.of(a, b, c).map(String::toUpperCase) // 将每个字符串转换为大写.forEach(System.out::println);Stream.of(1, 2, 2, 3, 4, 4).distinct().forEach(System.out::println); // 输出 1, 2, 3, 4Stream.of(5, 3, 1, 4, 2).sorted() // 默认升序排序.forEach(System.out::println);Stream.of(1, 2, 3, 4, 5).limit(3) // 只保留前 3 个元素.forEach(System.out::println);Stream.of(1, 2, 3, 4, 5).skip(2) // 跳过前 2 个元素.forEach(System.out::println); // 输出 3, 4, 5Stream().map()//将流中的每一个数据转化为另一种形式并返回新流
//1数据转化
ListString strings Arrays.asList(apple, banana, cherry);
ListString upperCaseStrings strings.stream().map(String::toUpperCase)//转化为大写.collect(Collectors.toList());
System.out.println(Upper Case Strings: upperCaseStrings);Upper Case Strings: [APPLE, BANANA, CHERRY]//2数据处理
ListInteger numbers Arrays.asList(1, 2, 3, 4, 5);
ListInteger squaredNumbers numbers.stream().map(n - n * n)//获得平方.collect(Collectors.toList());
System.out.println(Squared Numbers: squaredNumbers);Squared Numbers: [1, 4, 9, 16, 25]//3复杂对象转化
class User {String name;int age;User(String name, int age) {this.name name;this.age age;}public String getName() {return name;}
}ListUser users Arrays.asList(new User(Alice, 23),new User(Bob, 17),new User(Charlie, 25)
);ListString userNames users.stream().map(User::getName)//获得对象里的名字.collect(Collectors.toList());//并以list返回
System.out.println(User Names: userNames);User Names: [Alice, Bob, Charlie] 常见终结方法 终结方法会结束流的操作并返回一个结果如基本类型double,string等,或对象List,map等特殊值void等 Stream.of(a, b, c).forEach(System.out::println);ListString list Stream.of(a, b, c).collect(Collectors.toList());Stream().reduce()
int sum Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);
reduce 方法允许我们将流中的元素组通过操作求和、求积、连接字符串等合成一个单一的结果。它接收两个参数
初始值identity归约操作的初始值如果流为空则返回该值。
累加器accumulator用于将流中的元素累积到一个结果中的函数。
求和为例
public class ReduceExamples {public static void main(String[] args) {ListInteger numbers Arrays.asList(1, 2, 3, 4, 5);// 使用 reduce 求和int sum numbers.stream().reduce(0, (a, b) - a b);//0为初始值如果流中没有数据返回的值//(a, b) - a b是将前一个累积值a加上当前流上的数据bSystem.out.println(Sum of numbers: sum); // 输出Sum of numbers: 15}
}复杂对象苍穹外卖为例
//计算规定天数内的所有订单总量
Integer allOrder orderList.stream().reduce(Integer::sum).get();//其中orderList是每天的订单总量这里将每天订单量求和了。//如果集合里存的是对象若想获得对象里的其中值和
public class ReduceExamples {public static void main(String[] args) {ListProduct products Arrays.asList(new Product(iPhone, 999.99),new Product(Laptop, 1499.99),new Product(Headphones, 199.99));// 使用 reduce 求产品价格总和double totalPrice products.stream().mapToDouble(Product::getPrice)//通过 mapToDouble 将 Product 对象映射为其价格.reduce(0, Double::sum);System.out.println(Total price of products: totalPrice); // 输出Total price of products: 2699.97}
}long count Stream.of(a, b, c).count();//数据数量boolean hasA Stream.of(a, b, c).anyMatch(s - s.equals(a));//数据中是否有满足的boolean allMatch Stream.of(1, 2, 3).allMatch(n - n 5);//数据是否全部满足OptionalString first Stream.of(a, b, c).findFirst();//数据如果有返回第一个