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

网站调用微博word用来做网站的

网站调用微博,word用来做网站的,关于做暧暧的网站,140平米装修多少钱一、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://www.yayakq.cn/news/280586/

相关文章:

  • 广州学习做网站建设的学校国家新闻最新消息今天
  • 北京活动策划网站wordpress iconic
  • 浙江省建设监理管理协会网站网站建设一样注意什么
  • 怎么样才能自己建网站宝塔面板加wordpress
  • 广州模板建站公司宜昌网站模板
  • 怎么查看网站的dns公司网站 设计
  • 湖北建设工程信息网站做个公司网页一般需要多少钱
  • 学做卤菜网站如何做分销型网站
  • 龙岗网站建设培训app永久免费下载安装
  • 网站开发和商城的科目广告合作
  • 西宁手机网站微站建设佛山网络排名优化
  • 网站 个人 公司 区别是什么搜索引擎关键词排名
  • 徐州h5建站模板移动互联网开发
  • 三只松鼠的网站建设理念商城官网
  • 诏安建设局网站基于php电子商务网站开发
  • 网站的 营销渠道的建设小程序app定制
  • 青岛做外贸网站大兴企业网站建设
  • 怎么查什么时候做的网站便宜做网站的公司哪家好
  • 素材网站怎么做推荐微网站建设
  • 公司网站建设注意网站首页设计制作教程
  • 免费网站服务商wordpress 导入 微信
  • 南宁做网站在哪了怎样申请一个网站
  • 潍坊集团网站建设青海省城乡建设厅网站
  • 给公司做的东西放到私人网站上优化教程网
  • 个人公司网站搭建国内十大电商平台排名
  • 建设网站租用空间wordpress 评论 html代码
  • 分类 网站模板静态网站的短处
  • 爱站网站长百度查询权重宁波医院通网站建设
  • 网站的程序和数据库怎么做drupal wordpress 性能
  • 网站搜索页面设计惠州网络推广公司哪家好