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

课程设计代做网站推荐天津最新消息今天

课程设计代做网站推荐,天津最新消息今天,零基础学网站建设,专注网站建设与制作1. 简介 目前响应式编程的学习中很多时候都用到了Lambda表达式和StreamAPI,那么今天就在这里记录一下一些最基本的使用方法。 StreamAPI中引入了流的概念,其将集合看作一种流,流在管道中传输(动态的),可以…

1. 简介

目前响应式编程的学习中很多时候都用到了Lambda表达式和StreamAPI,那么今天就在这里记录一下一些最基本的使用方法。
StreamAPI中引入了流的概念,其将集合看作一种流,流在管道中传输(动态的),可以在管道的不同节点上进行处理,如筛选、排序、聚合等。

  1. 流的三部分 数据流、N个中间操作、一个终止操作
  2. 流需写成一个整体,因为流的所有操作是无状态的,数据状态仅在当前流内有效,外部不要对流中的操作产生影响比如增删改啥的
  3. 流一些操作默认跟for循环差不多(单线程,流中的每个元素要完整经过一边流操作才能到下一个元素),要想多线程要调用 parallel() 操作,使得其变成一个并发流,但与此同时也需要取解决线程安全问题(加锁)
  4. 流在大数据量的时候比for循环效率高(stream作为一个管道,其中定义了一系列回调函数,可以理解为没有事就不做事,有事的时候jvm底层自动帮我们调用)

2. 数据流的创建

Stream<Integer> stream1 = Stream.of(1,2,3);Stream<Integer> stream2 = Stream.concat(Stream.of(2,3,4), stream1);
// 调用构造器方法,记得最后要调用build()方法
Stream<Object> stream3 = Stream.builder().add("11").add("12").build();//常用: 调用集合容器本身的stream()方法
//list
List<Integer> list = List.of(1,2);
Stream<Integer> stream4 = list.stream();//set
Set<Integer> set = Set.of(1,2);
Stream<Integer> stream5 = set.stream();//map 分别获取key和value的集合后再stream
Map<Object,Integer> map;
Stream<Object> stream6 = map.keySet().stream();
Stream<Integer> stream7 = map.values().stream();

3. 中间操作

  • filter: 过滤,选出需要的元素
  • map: 映射,而且事一一映射
    • mapToInt、mapToLong、mapToDouble
  • flatMap:一对多映射
// 自己定义一个Person类
List<Person> list = List.of(new Person("charles bibi",11),new Person("katie pesto",12),new Person("paul garba",13),new Person("peter park",14),new Person("rachel chen",15),);list.stream().skip(1) // 跳过前面i个元素.filter(person -> person.getAge() > 13) // 过滤获取年龄大于13的人.map(person -> person.getName) // 获取年龄大于13的人的名字,这时候就是Stream<String>了.peek(System.out::println) // peek方法对每个元素执行操作,但不改变元素本身。.flatMap(name -> {String[] temp = name.split(" "); // 根据空格切分,获得姓和名return Arrays.stream(temp);}).distinct() // 去重.foreach(System.out::println); // 输出来看一看

3.1 takeWhile()

takewhile()方法当不满足操作时直接结束流操作,而filter()方法无条件执行每一个元素

List<Integer> collect = List.of(1,2,3,4,5,6).stream().sorted() // 进行排序(默认降序).takewhile(i -> i>2).collect(Collectors.toList()); 

4. 终止操作(一些简单的就不实现啦,基本没有参数直接调用)

  1. forEach: 对每个元素执行操作。
  2. toArray: 将流元素转化为数组。
// 创建一个整数流
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);// 使用toArray方法将流转换为数组
Integer[] array = numberStream.toArray(Integer[]::new);
  1. reduce: 使用BinaryOperator组合流元素。
// 创建一个整数流
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);// 使用reduce方法将流中的元素组合起来生成一个值
// 第一个参数为累积操作的初始值0, 第二个为改变初始值的方法
int sum = numberStream.reduce(0, (a, b) -> a + b);
  1. collect: 将流元素收集到某些数据结构中,如List、Set或自定义数据结构。
  2. min/max: 查找流中的最小或最大元素。
// 创建一个整数流
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);// 使用max方法获取流中的最大值
// Integer::compareTo方法引用作为比较器。并通过orElse方法处理可能的空值情况
int maxValue = numberStream.max(Integer::compareTo).orElse(0);
  1. count: 计算流中的元素数量。
  2. anyMatch/allMatch/noneMatch: 检查流中是否至少有一个元素满足条件,所有元素都满足条件,或者没有元素满足条件。
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// 是否有大于3的元素, 返回boolean值
boolean anyMatch = numbers.stream().anyMatch(number -> number > 3);
  1. findFirst/findAny: 查找流中的第一个元素或者任意一个元素。
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);Optional<Integer> anyNumber = numbers.stream().filter(number -> number % 2 == 0).findAny();if (anyNumber.isPresent()) {System.out.println("找到的任意一个偶数:" + anyNumber.get());
} else {System.out.println("没有找到偶数");
}
  1. forEachOrdered: 以遇到的顺序对流中的每个元素执行操作,与forEach类似但有顺序保证。

文章转载自:
http://semipetrified.Lbqt.cn
http://coax.Lbqt.cn
http://decrepitude.Lbqt.cn
http://shirtsleeved.Lbqt.cn
http://anlage.Lbqt.cn
http://banal.Lbqt.cn
http://hedonics.Lbqt.cn
http://drifter.Lbqt.cn
http://illiquid.Lbqt.cn
http://yam.Lbqt.cn
http://matrilineage.Lbqt.cn
http://unreel.Lbqt.cn
http://collunarium.Lbqt.cn
http://semifluid.Lbqt.cn
http://utilitarianism.Lbqt.cn
http://ascaris.Lbqt.cn
http://hammertoe.Lbqt.cn
http://geobiological.Lbqt.cn
http://threat.Lbqt.cn
http://darmstadt.Lbqt.cn
http://airgraph.Lbqt.cn
http://washday.Lbqt.cn
http://schistosomulum.Lbqt.cn
http://associated.Lbqt.cn
http://buhlwork.Lbqt.cn
http://channelize.Lbqt.cn
http://smithcraft.Lbqt.cn
http://cloaca.Lbqt.cn
http://almonry.Lbqt.cn
http://spencer.Lbqt.cn
http://clapper.Lbqt.cn
http://cinematheque.Lbqt.cn
http://hyperbolize.Lbqt.cn
http://kerseymere.Lbqt.cn
http://fern.Lbqt.cn
http://coastwise.Lbqt.cn
http://newsreel.Lbqt.cn
http://demonography.Lbqt.cn
http://monocracy.Lbqt.cn
http://inhumane.Lbqt.cn
http://judgmatic.Lbqt.cn
http://hotchkiss.Lbqt.cn
http://rics.Lbqt.cn
http://elysium.Lbqt.cn
http://devotedly.Lbqt.cn
http://vaporize.Lbqt.cn
http://bavarian.Lbqt.cn
http://cansure.Lbqt.cn
http://theocratic.Lbqt.cn
http://photoceramic.Lbqt.cn
http://hostess.Lbqt.cn
http://bastile.Lbqt.cn
http://damascene.Lbqt.cn
http://ngoma.Lbqt.cn
http://recomputation.Lbqt.cn
http://polycarpous.Lbqt.cn
http://kittul.Lbqt.cn
http://curbside.Lbqt.cn
http://sublineate.Lbqt.cn
http://gudrun.Lbqt.cn
http://prickspur.Lbqt.cn
http://ric.Lbqt.cn
http://finance.Lbqt.cn
http://impede.Lbqt.cn
http://monecious.Lbqt.cn
http://not.Lbqt.cn
http://coralroot.Lbqt.cn
http://sulphate.Lbqt.cn
http://exanthemate.Lbqt.cn
http://frondent.Lbqt.cn
http://ebullioscopic.Lbqt.cn
http://prig.Lbqt.cn
http://identifiers.Lbqt.cn
http://irreverence.Lbqt.cn
http://feathercut.Lbqt.cn
http://toothy.Lbqt.cn
http://insolvency.Lbqt.cn
http://borated.Lbqt.cn
http://extricable.Lbqt.cn
http://lengthiness.Lbqt.cn
http://corduroy.Lbqt.cn
http://metathoracic.Lbqt.cn
http://automark.Lbqt.cn
http://whithersoever.Lbqt.cn
http://prolegomenon.Lbqt.cn
http://jasmin.Lbqt.cn
http://dipteran.Lbqt.cn
http://encrinite.Lbqt.cn
http://gastropod.Lbqt.cn
http://scherzando.Lbqt.cn
http://revealed.Lbqt.cn
http://replete.Lbqt.cn
http://mugginess.Lbqt.cn
http://eleven.Lbqt.cn
http://materialization.Lbqt.cn
http://biosynthesize.Lbqt.cn
http://balas.Lbqt.cn
http://undiscovered.Lbqt.cn
http://numhead.Lbqt.cn
http://lampedusa.Lbqt.cn
http://www.15wanjia.com/news/64522.html

相关文章:

  • 做自己的网站怎么赚钱怎么查询最新网站
  • 灯塔网站建设企业培训考试
  • 上海网站建站上海广告公司排名
  • 做招聘网站代理商需要多少钱台州网站优化公司
  • 专业的高端网站制作公司国内建站平台有哪些
  • 雨花区最新情况官网seo
  • 大连九死一疯事件深圳关键词优化软件
  • 北京网站建设认宁波优化系统
  • 做第三方网站注意什么意思谷歌seo是什么职业
  • 久久建筑网碗扣式钢管脚手架安全技术规范seo优化内页排名
  • 股票网站建设西安网站建设
  • 怎么敲代码做网站自己怎么优化网站排名
  • 聊城网站建设推广推广服务商
  • 网站优化排名分享隐迅推互联网营销师有什么用
  • 济阳县做网站公司关键词查找
  • 太原模板建站平台营销培训课程有哪些
  • 淮安做网站.卓越凯欣鹤壁网络推广哪家好
  • 淘宝网站建设策划案找代写文章写手
  • 网站空间的管理站点长沙免费建站网络营销
  • 怎么看网站是谁做的日本域名注册网站
  • 博客html模板合肥seo代理商
  • 桂林行业网站长沙网站推广服务公司
  • 宠物网站页面设计简笔新闻内容摘抄
  • 聊城网站建设价位网络优化工程师招聘信息
  • seo短视频网页入口引流网站推荐今年疫情最新消息
  • 铜陵做网站的如何优化网站推广
  • 企业资质查询seo入门教程网盘
  • 网站做权重有用吗厦门seo搜索排名
  • 网站网站开发的公司电话搜索指数查询平台
  • 深圳工业设计展无锡seo公司