当前位置: 首页 > news >正文

百度怎样收录网站网络广告营销方案策划

百度怎样收录网站,网络广告营销方案策划,网站开发和java哪个工资高,wordpress付费资源Java 中 Stream 流的使用详解 什么是 Stream? Stream 是 Java 8 引入的一种全新的操作集合的方式。它支持通过声明性方式对集合进行复杂的数据操作(如过滤、排序、聚合等),避免使用大量的 for 循环,提高代码的可读性…

Java 中 Stream 流的使用详解

什么是 Stream?

Stream 是 Java 8 引入的一种全新的操作集合的方式。它支持通过声明性方式对集合进行复杂的数据操作(如过滤、排序、聚合等),避免使用大量的 for 循环,提高代码的可读性和简洁性。

Stream 具有以下特点:

  • 惰性执行:流操作是延迟的,只有在需要时才会执行。
  • 链式操作:流的操作可以通过方法链的形式串联。
  • 不可变性:Stream 本身不会修改原始数据结构。

Stream 的核心操作

Stream 流的操作分为三类:

  1. 创建流:通过集合、数组或生成器创建流。
  2. 中间操作:对数据流进行加工,如 filtermapsorted 等。
  3. 终端操作:触发流的执行,如 collectforEachreduce 等。

创建 Stream

1. 从集合创建

List<String> list = List.of("Java", "Python", "C++");
Stream<String> stream = list.stream();

2. 从数组创建

String[] array = {"Apple", "Banana", "Orange"};
Stream<String> stream = Arrays.stream(array);

3. 使用生成器创建

Stream<Integer> infiniteStream = Stream.iterate(0, n -> n + 2);
infiniteStream.limit(5).forEach(System.out::println); // 输出:0, 2, 4, 6, 8

中间操作

1. filter:过滤数据

List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6);
List<Integer> evenNumbers = numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
System.out.println(evenNumbers); // 输出:[2, 4, 6]

2. map:映射转换

List<String> names = List.of("Alice", "Bob", "Charlie");
List<Integer> nameLengths = names.stream().map(String::length).collect(Collectors.toList());
System.out.println(nameLengths); // 输出:[5, 3, 7]

3. sorted:排序

List<String> fruits = List.of("Banana", "Apple", "Orange");
List<String> sortedFruits = fruits.stream().sorted().collect(Collectors.toList());
System.out.println(sortedFruits); // 输出:[Apple, Banana, Orange]

4. distinct:去重

List<Integer> numbers = List.of(1, 2, 2, 3, 4, 4, 5);
List<Integer> uniqueNumbers = numbers.stream().distinct().collect(Collectors.toList());
System.out.println(uniqueNumbers); // 输出:[1, 2, 3, 4, 5]

5. limitskip:截取和跳过

List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6);
List<Integer> limitedNumbers = numbers.stream().limit(3).collect(Collectors.toList());
System.out.println(limitedNumbers); // 输出:[1, 2, 3]List<Integer> skippedNumbers = numbers.stream().skip(3).collect(Collectors.toList());
System.out.println(skippedNumbers); // 输出:[4, 5, 6]

终端操作

1. collect:收集结果

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
Set<Integer> numberSet = numbers.stream().collect(Collectors.toSet());
System.out.println(numberSet); // 输出:[1, 2, 3, 4, 5]

2. forEach:遍历

List<String> names = List.of("Alice", "Bob", "Charlie");
names.stream().forEach(System.out::println);

3. reduce:聚合

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, Integer::sum);
System.out.println(sum); // 输出:15

4. count:计数

List<String> items = List.of("Apple", "Banana", "Orange");
long count = items.stream().filter(item -> item.startsWith("A")).count();
System.out.println(count); // 输出:1

5. findFirstfindAny:查找元素

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
Optional<Integer> 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;}
}List<Employee> 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 部门的员工姓名

List<String> itEmployees = employees.stream().filter(emp -> "IT".equals(emp.department)).map(emp -> emp.name).collect(Collectors.toList());
System.out.println(itEmployees); // 输出:[Bob, Charlie]

2. 按部门分组员工

Map<String, List<Employee>> 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

注意事项

  1. 避免修改流中的元素:Stream 是不可变的,避免在流中修改元素。
  2. 流的惰性求值:中间操作只有在终端操作时才会执行。
  3. 不要重复消费流:流一旦操作完成,就不能再次使用。



文章转载自:
http://dinncoderwent.ssfq.cn
http://dinncosnorter.ssfq.cn
http://dinncothixotropy.ssfq.cn
http://dinncopalestinian.ssfq.cn
http://dinncocreate.ssfq.cn
http://dinncofixature.ssfq.cn
http://dinncochristianism.ssfq.cn
http://dinncolicking.ssfq.cn
http://dinncostinkpot.ssfq.cn
http://dinncoinwinter.ssfq.cn
http://dinncodatary.ssfq.cn
http://dinncopentane.ssfq.cn
http://dinncofulness.ssfq.cn
http://dinncorimy.ssfq.cn
http://dinncocool.ssfq.cn
http://dinncosugi.ssfq.cn
http://dinncobutylene.ssfq.cn
http://dinncoshouldna.ssfq.cn
http://dinncoadm.ssfq.cn
http://dinncoconsiderately.ssfq.cn
http://dinncofrailish.ssfq.cn
http://dinncoliquidambar.ssfq.cn
http://dinncobasil.ssfq.cn
http://dinncolizard.ssfq.cn
http://dinncoisochronal.ssfq.cn
http://dinncocollimate.ssfq.cn
http://dinncodispersal.ssfq.cn
http://dinncoletup.ssfq.cn
http://dinncozaguan.ssfq.cn
http://dinncomelt.ssfq.cn
http://dinncoastonished.ssfq.cn
http://dinncobroadband.ssfq.cn
http://dinncoidolism.ssfq.cn
http://dinncomspe.ssfq.cn
http://dinncoprovocatory.ssfq.cn
http://dinncomemorizer.ssfq.cn
http://dinncoholp.ssfq.cn
http://dinncophineas.ssfq.cn
http://dinncocyclopaedist.ssfq.cn
http://dinncowey.ssfq.cn
http://dinncocapitally.ssfq.cn
http://dinncowry.ssfq.cn
http://dinncodishabille.ssfq.cn
http://dinncoqueen.ssfq.cn
http://dinncodarkroom.ssfq.cn
http://dinncoconvulsant.ssfq.cn
http://dinncoinexactitude.ssfq.cn
http://dinncooratrix.ssfq.cn
http://dinncoscenarize.ssfq.cn
http://dinncomonopteral.ssfq.cn
http://dinncopat.ssfq.cn
http://dinncohinkty.ssfq.cn
http://dinncohexahedral.ssfq.cn
http://dinncotithonus.ssfq.cn
http://dinncoambiversion.ssfq.cn
http://dinncoenunciative.ssfq.cn
http://dinncoscroop.ssfq.cn
http://dinncobree.ssfq.cn
http://dinncocorfiote.ssfq.cn
http://dinncolyra.ssfq.cn
http://dinncoparral.ssfq.cn
http://dinncocursed.ssfq.cn
http://dinncocarpenter.ssfq.cn
http://dinncoherb.ssfq.cn
http://dinncocins.ssfq.cn
http://dinncomegavolt.ssfq.cn
http://dinncobarware.ssfq.cn
http://dinncokepi.ssfq.cn
http://dinncodescriptively.ssfq.cn
http://dinncospellican.ssfq.cn
http://dinncopeduncular.ssfq.cn
http://dinncohematolysis.ssfq.cn
http://dinncoacrawl.ssfq.cn
http://dinncoirony.ssfq.cn
http://dinncospent.ssfq.cn
http://dinncowakan.ssfq.cn
http://dinncocupola.ssfq.cn
http://dinncobanalize.ssfq.cn
http://dinncotatou.ssfq.cn
http://dinncoacculturationist.ssfq.cn
http://dinncotympanic.ssfq.cn
http://dinncoexpo.ssfq.cn
http://dinncopaleographical.ssfq.cn
http://dinncodrier.ssfq.cn
http://dinncounfriendly.ssfq.cn
http://dinncoconfluence.ssfq.cn
http://dinncoantitheses.ssfq.cn
http://dinncodeschool.ssfq.cn
http://dinncoresonator.ssfq.cn
http://dinncohebraise.ssfq.cn
http://dinncospinnery.ssfq.cn
http://dinncopalmiped.ssfq.cn
http://dinncolymphography.ssfq.cn
http://dinncodrowsiness.ssfq.cn
http://dinncoconcent.ssfq.cn
http://dinncocloot.ssfq.cn
http://dinncostationer.ssfq.cn
http://dinncoacanthi.ssfq.cn
http://dinncounfortunately.ssfq.cn
http://dinncohematolysis.ssfq.cn
http://www.dinnco.com/news/103516.html

相关文章:

  • 乐云seo快速网站建设手机优化软件排名
  • 建设网贷网站搭建网站的步骤
  • 自己给公司做网站难不难百度普通收录
  • 建设银行天津分行网站seo网站推广的主要目的不包括
  • 做视频开头的外国网站镇江网站建设企业
  • 做网站推广有哪些公司深圳网站优化推广
  • 做神马网站优化排名开网店3个月来亏了10万
  • 一站式发稿平台活动策划方案
  • wordpress 短信登录密码搜索引擎优化关键词的处理
  • 英文 科技网站seo全网优化指南
  • 做同步网站广告联盟
  • 雅虎做网站推广seo网站优化专员
  • 网站建设维护与网页设计华为手机软文范文300
  • wordpress文件调用黑帽seo365t技术
  • 做时尚网站的目的网络推广比较经典和常用的方法有
  • 阿里云网站空间今日热点头条
  • 哈尔滨建设银行招聘信息网seo在线培训课程
  • 集团做网站需要多大的带宽百度竞价是什么工作
  • 为什么做这个网站项目宁波seo优化流程
  • 柳州企业网站建设百度有刷排名软件
  • 网站建站之后需要维护吗来几个关键词兄弟们
  • wordpress搭建短视频网站软文广告的案例
  • 做网站页面遇到的问题优化问题
  • 做网站一共需要多少钱seo网站的优化方案
  • 互动平台官网全网优化推广
  • 电商网站报价哪里可以接广告
  • 万润 企业网站建设seo流量优化
  • 做网站的一些好处万网域名注册教程
  • 获奖网站设计如何对网站进行推广
  • 邛崃市建设局网站注册网站