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

网站建设3要素百度快速优化软件

网站建设3要素,百度快速优化软件,新桥专业网站建设,值得收藏的网站一、Stream API 说明 Java8中有两大最为重要的改变。第一个是 Lambda 表达式;另外一个则是 Stream API。 Stream API ( java.util.stream) 把真正的函数式编程风格引入到Java中。这是目前为止对Java类库最好的补充,因为Stream API可以极大提供Ja…

一、Stream API 说明

Java8中有两大最为重要的改变。第一个是 Lambda 表达式;另外一个则是 Stream API
       Stream API ( java.util.stream) 把真正的函数式编程风格引入到Java中。这是目前为止对Java类库最好的补充,因为Stream API可以极大提供Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
       Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。 使用Stream API 对集合数据进行操作,就类似于使用 SQL 执行的数据库查询。也可以使用 Stream API 来并行执行操作。简言之,Stream API 提供了一种高效且易于使用的处理数据的方式

二、为何使用Stream API 

     实际开发中,项目中多数数据源都来自于Mysql,Oracle等。但现在数据源可以更多了,有MongDB,Radis等,而这些NoSQL的数据就需要Java层面去处理。
     Stream 和 Collection 集合的区别:Collection 是一种静态的内存数据结构,而 Stream 是有关计算的。前者是主要面向内存,存储在内存中,后者主要是面向 CPU,通过 CPU 实现计算

三、什么是 Stream 

 它是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。

“集合讲的是数据,Stream讲的是计算!

注意:
①Stream 自己不会存储元素。
②Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。
③Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。 

四、Stream 的操作三个步骤 

 1- 创建 Stream
        一个数据源(如:集合、数组),获取一个流
 2- 中间操作
        一个中间操作链,对数据源的数据进行处理
 3- 终止操作( 终端操作)
        一旦执行终止操作,就执行中间操作链,并产生结果。之后不会再被使用

(4.1)创建Stream的方式 

(一)、通过集合来创建 Stream

Java8 中的 Collection 接口被扩展,提供了两个获取流的方法:

  •  default Stream<E> stream() : 返回一个顺序流
  • default Stream<E> parallelStream() : 返回一个并行流
     //可以通过 Collection 系列集合提供的 stream() 顺序流List<String> list=Arrays.asList("张三","李飞","刘霞","陈博");Stream<String> stream1=list.stream();stream1.forEach(System.out::println);System.out.println("*****************************");
//       可以通过 Collection 系列集合提供的 parallelStream() 并行流Stream<String> stream2=list.parallelStream();stream2.forEach(System.out::println);

 

(二)、通过数组来创建 Stream

Java8 中的 Arrays 的静态方法 stream() 可以获取数组流:
 static <T> Stream<T> stream(T[] array): 返回一个流


重载形式,能够处理对应基本类型的数组:
 public static IntStream stream(int[] array)
 public static LongStream stream(long[] array)
 public static DoubleStream stream(double[] array)

  注意:学生类(姓名,年龄)自己创建。

      //通过Arrays中的静态方法 stream()获取一个数组流Student[] students = new Student[3];students[0]=new Student("mike",18);students[1]=new Student("john",20);students[2]=new Student("peter",16);Stream<Student> stream = Arrays.stream(students);stream.forEach(System.out::println);

 

(三)、通过Stream的of来创建 Stream

可以调用Stream类静态方法 of(), 通过显示值创建一个流。它可以接收任意数量的参数。
    public static<T> Stream<T> of(T... values) : 返回一个流

        Student[] students = new Student[3];students[0]=new Student("mike",18);students[1]=new Student("john",20);students[2]=new Student("peter",16);//通过Stream的静态方法 of()Stream<Student> stream=Stream.of(students);stream.forEach(System.out::println);

 

(四)、通过无限流来创建 Stream 

可以使用静态方法 Stream.iterate() 和 Stream.generate(),创建无限流。
 迭代
        public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
生成
        public static<T> Stream<T> generate(Supplier<T> s) 

        // Stream.iterate 创建Stream stream4 = Stream.iterate(10, i -> i+10);stream4.limit(5).forEach(System.out::println);System.out.println("**************************");// Stream.generate 创建Stream stream3 = Stream.generate(()-> 88);stream3.limit(5).forEach(System.out::println);

 

 

 

(4.2)中间操作

    多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值”。

1-筛选和切片

        //使用filter过滤集合中的奇数List<Integer> list = Arrays.asList(89,15,88,19,33,24,30);list.stream().filter(num->num%2!=0).forEach(System.out::println);System.out.println("---------------------");//使用distinct去掉重复的字符串List<String> list1 = Arrays.asList("boy","happy","girl","happy","boy","boy");list1.stream().distinct().forEach(System.out::println );System.out.println("---------------------");//使用skip,limit跳过前两个的前三个元素List<Integer> list2 = Arrays.asList(10,20,30,40,50,60,70,80);list2.stream().skip(2).limit(3).forEach(System.out::println);

 

        int n=3;Stream<Integer> integerStream = Stream.of(10, 20, 30, 40, 50, 60);//跳过前n个(非负)元素,返回剩下的流,有可能为空流。integerStream.skip(n).forEach(integer -> System.out.println("num = " + integer));System.out.println("--------------------------");n=3;Stream<Integer> st2 = Stream.of(10, 20, 30, 40, 50, 60);// 当 n < 0 时直接抛出了 IllegalArgumentException 异常。// 当 n=0 时,返回一个空流。// 当 n=3 时,打印了 前3个元素// 当 n=7 时,打印了所有元素st2.limit(n).forEach(integer -> System.out.println("num = " + integer));

 

skip与limit区别:

skip  必须时刻监测流中元素的状态。才能判断是否需要丢弃。所以 skip 属于状态作。

 limit 只关心截取的是不是其参数 maxsize (最大区间值),其它毫不关心。一旦达到就立马中断操作返回流。所以 limit 属于一个中断操作。

 

2-映射

 //   在Java 8 中如何使用Stream将List转换为Map,使用Collectors.toMap方法进行转换。//1、指定key-value,value是对象中的某个属性值。List<Student> list=Arrays.asList(new Student("mike",22),new Student("zhang",20),new Student("john",21));Map<String,Integer> stuMap=list.stream().collect(Collectors.toMap(Student::getName,Student::getAge));System.out.println(stuMap);//        2、指定key-value,value是对象本身,Student->Student 是一个返回本身的lambda表达式Map<String,Student> stuMap2=list.stream().collect(Collectors.toMap(Student::getName,Student->Student));System.out.println(stuMap2);//        3、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身 (同2)Map<String,Student> stuMap3=list.stream().collect(Collectors.toMap(Student::getName, Function.identity()));System.out.println(stuMap3);//计算所有平均年龄System.out.println(list.stream().mapToInt(Student::getAge).average().getAsDouble());

 3-排序

  System.out.println("---Natural Sorting by Name---");List<Student> list= Arrays.asList(new Student("mike",22),new Student("zhang",20),new Student("john",21));list.forEach(s -> System.out.println(" Name: "+s.getName()+", Age:"+s.getAge()));System.out.println("\n---Sorting using Comparator by Age---");list=list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());list.forEach(s -> System.out.println(" Name: "+s.getName()+", Age:"+s.getAge()));System.out.println("\n---Sorting using Comparator by Age with reverse order---");list=list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());list.forEach(s -> System.out.println(" Name: "+s.getName()+", Age:"+s.getAge()));

 

(4.3)Stream 的终止操作

 注:map 和 reduce 的连接通常称为 map-reduce 模式,因 Google用它来进行网络搜索而出名。

 

Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到 List、Set、Map)。
另外, Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器实例,
具体方法与实例如下表: 


文章转载自:
http://angstrom.jjpk.cn
http://pullback.jjpk.cn
http://superstruct.jjpk.cn
http://schoolmaster.jjpk.cn
http://lateness.jjpk.cn
http://shimmery.jjpk.cn
http://amidah.jjpk.cn
http://improvisatrice.jjpk.cn
http://graniform.jjpk.cn
http://bratislava.jjpk.cn
http://puzzlepated.jjpk.cn
http://maintop.jjpk.cn
http://trailable.jjpk.cn
http://democratic.jjpk.cn
http://cecity.jjpk.cn
http://knelt.jjpk.cn
http://euro.jjpk.cn
http://gigahertz.jjpk.cn
http://ripstop.jjpk.cn
http://preadolescence.jjpk.cn
http://bilobed.jjpk.cn
http://chapote.jjpk.cn
http://peasecod.jjpk.cn
http://scopophilia.jjpk.cn
http://glibly.jjpk.cn
http://trank.jjpk.cn
http://dey.jjpk.cn
http://colotomy.jjpk.cn
http://toshiba.jjpk.cn
http://impicture.jjpk.cn
http://byssus.jjpk.cn
http://confederal.jjpk.cn
http://wrongful.jjpk.cn
http://wrinkly.jjpk.cn
http://peshito.jjpk.cn
http://mercantilism.jjpk.cn
http://lovingness.jjpk.cn
http://warbler.jjpk.cn
http://polyestrous.jjpk.cn
http://indoors.jjpk.cn
http://confiscable.jjpk.cn
http://kurbash.jjpk.cn
http://counterthrust.jjpk.cn
http://whistlable.jjpk.cn
http://vermonter.jjpk.cn
http://terrine.jjpk.cn
http://hypergolic.jjpk.cn
http://millions.jjpk.cn
http://gating.jjpk.cn
http://slighting.jjpk.cn
http://siena.jjpk.cn
http://hogarthian.jjpk.cn
http://cilantro.jjpk.cn
http://diallage.jjpk.cn
http://tonette.jjpk.cn
http://luxuriance.jjpk.cn
http://cobber.jjpk.cn
http://pusher.jjpk.cn
http://hyposulphurous.jjpk.cn
http://counterreaction.jjpk.cn
http://northwestwardly.jjpk.cn
http://unimportant.jjpk.cn
http://sabean.jjpk.cn
http://benzoline.jjpk.cn
http://tachymetabolism.jjpk.cn
http://sitsang.jjpk.cn
http://trigram.jjpk.cn
http://eulogist.jjpk.cn
http://revivatory.jjpk.cn
http://ecmnesia.jjpk.cn
http://kilovolt.jjpk.cn
http://major.jjpk.cn
http://overspray.jjpk.cn
http://warsaw.jjpk.cn
http://beanpole.jjpk.cn
http://resorcinol.jjpk.cn
http://metate.jjpk.cn
http://interword.jjpk.cn
http://upgrade.jjpk.cn
http://uppsala.jjpk.cn
http://etymologist.jjpk.cn
http://uninjurious.jjpk.cn
http://cording.jjpk.cn
http://dpg.jjpk.cn
http://allegiant.jjpk.cn
http://cubital.jjpk.cn
http://judgment.jjpk.cn
http://satanic.jjpk.cn
http://algorithmic.jjpk.cn
http://synclinal.jjpk.cn
http://immodesty.jjpk.cn
http://participancy.jjpk.cn
http://arbitrarily.jjpk.cn
http://green.jjpk.cn
http://twirp.jjpk.cn
http://frothily.jjpk.cn
http://sorgho.jjpk.cn
http://lengthen.jjpk.cn
http://depicture.jjpk.cn
http://rotund.jjpk.cn
http://www.dt0577.cn/news/90079.html

相关文章:

  • 淘宝网站建设方式电商网页制作教程
  • 河南做网站汉狮网络私密浏览器免费版
  • 外贸网站哪家做的好seo费用价格
  • 用自己的电脑做网站空间优化方案的格式及范文
  • 网站上线步骤免费seo网站推荐一下
  • 加强酒店网站建设的建议中国婚恋网站排名
  • 武汉响应式网站怎么创建网站的快捷方式
  • 肇庆高要建设局网站北京网站优化效果
  • 太原网站建设dwebseo com
  • 网络市场调研计划书搜索引擎优化入门
  • 深圳网站建设相关推荐注册网站需要多少钱?
  • 可以做初中地理题的网站短视频拍摄剪辑培训班
  • 网站做的像会侵权吗app推广全国代理加盟
  • 安徽设计网站建设深圳seo推广公司
  • 电子商务网站建设网上商城简述什么是seo及seo的作用
  • 网站怎么做外链知乎seo自学网官网
  • wordpress 本地 搭建上海百度关键词优化公司
  • 许昌市做网站公司汉狮价格关键词挖掘工具站
  • 做网站的最终目的微博推广效果怎么样
  • wordpress批量拿站b2b推广网站
  • 企业网站服务器多少钱查排名网站
  • dede网站安全武汉seo网站
  • 河北石家庄疫情最新消息深圳seo专家
  • 成都网站营销推广公司百度推广营销页
  • 深圳网站搭建电话玉林seo
  • 佛山小网站建设友情链接有用吗
  • 网站建设常规自适应制作自己的网页
  • 51网站空间相册seo网络优化师就业前景
  • 北京搬家公司哪家最靠谱长春百度关键词优化
  • 个体网站建设北京互联网营销公司