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

重庆制作网站首页医疗类网站备案

重庆制作网站首页,医疗类网站备案,推广seo公司,wordpress评论加图片Java基础系列文章 Java基础(一)#xff1a;语言概述 Java基础(二)#xff1a;原码、反码、补码及进制之间的运算 Java基础(三)#xff1a;数据类型与进制 Java基础(四)#xff1a;逻辑运算符和位运算符 Java基础(五)#xff1a;流程控制语句 Java基础(六)#xff1…Java基础系列文章 Java基础(一)语言概述 Java基础(二)原码、反码、补码及进制之间的运算 Java基础(三)数据类型与进制 Java基础(四)逻辑运算符和位运算符 Java基础(五)流程控制语句 Java基础(六)数组 Java基础(七)面向对象编程 Java基础(八)封装、继承、多态性 Java基础(九)Object 类的使用 Java基础(十)关键字static、代码块、关键字final Java基础(十一)抽象类、接口、内部类 Java基础(十二)枚举类 Java基础(十三)注解(Annotation) Java基础(十四)包装类 Java基础(十五)异常处理 Java基础(十六)String的常用API Java基础(十七)日期时间API Java基础(十八)java比较器、系统相关类、数学相关类 Java基础(十九)集合框架 Java基础(二十)泛型 Java基础(二十一)集合源码 Java基础(二十二)File类与IO流 Java基础(二十三)反射机制 Java基础(二十四)网络编程 Java基础(二十五)Lambda表达式、方法引用、构造器引用 Java基础(二十六)Java8 Stream流及Optional类 目录 一、Stream介绍1、什么是Stream2、Stream特点3、Stream的操作三个步骤 二、创建Stream实例1、通过集合创建Stream2、通过数组创建Stream3、通过Stream的of()创建Stream4、创建无限流 三、Stream中间操作1、筛选filter2、截断limit3、跳过skip4、去重distinct5、映射map/flatMap/mapToInt6、排序sorted7、peek 和 forEach 四、Stream终止操作1、匹配allMatch/anyMatch/noneMatch2、查找findFirst/findAny3、聚合max/min/count4、归约reduce5、收集(collect)5.1、归集toList/toSet/toMap5.2、统计counting/averaging/summing/maxBy/minBy5.3、分组partitioningBy/groupingBy5.4、接合joining 五、Optional 类1、构建Optional对象2、获取value值空值的处理3、处理value值空值不处理不报错 一、Stream介绍 1、什么是Stream Stream 是数据渠道用于操作数据源集合、数组等所生成的元素序列Stream和Collection集合的区别 Collection是一种静态的内存数据结构讲的是数据主要面向内存存储在内存中Stream是有关计算的讲的是计算面向CPU通过CPU实现计算 2、Stream特点 Stream自己不会存储元素Stream不会改变源对象。相反他们会返回一个持有结果的新StreamStream 操作是延迟执行的 这意味着他们会等到需要结果的时候才执行即一旦执行终止操作就执行中间操作链并产生结果 Stream一旦执行了终止操作就不能再调用其它中间操作或终止操作了 3、Stream的操作三个步骤 创建 Stream 一个数据源如集合、数组获取一个流 中间操作 每次处理都会返回一个持有结果的新Stream即中间操作的方法返回值仍然是Stream类型的对象因此中间操作可以是个操作链可对数据源的数据进行n次处理但是在终结操作前并不会真正执行 终止操作(终端操作) 终止操作的方法返回值类型就不再是Stream了因此一旦执行终止操作就结束整个Stream操作了一旦执行终止操作就执行中间操作链最终产生结果并结束Stream 二、创建Stream实例 1、通过集合创建Stream Java8中的Collection接口被扩展提供了两个获取流的方法 default StreamE stream() : 返回一个顺序流default StreamE parallelStream() : 返回一个并行流 Test public void test01(){ListString list Arrays.asList(a, b, c, d);//创建顺序流顺序执行StreamString stream list.stream();//创建并行流多线程并行执行速度快StreamString parallelStream list.parallelStream(); }2、通过数组创建Stream Java8 中的 Arrays 的静态方法 stream() 可以获取数组流 public static T StreamT stream(T[] array): 返回一个流public static IntStream stream(int[] array)public static LongStream stream(long[] array)public static DoubleStream stream(double[] array) Test public void test02(){String[] arr {hello,world};StreamString stream Arrays.stream(arr); }Test public void test03(){int[] arr {1,2,3,4,5};IntStream stream Arrays.stream(arr); }3、通过Stream的of()创建Stream 可以调用Stream类静态方法 of(), 通过显示值创建一个流它可以接收任意数量的参数 public staticT StreamT of(T… values) : 返回一个流 Test public void test04(){StreamInteger stream Stream.of(1,2,3,4,5);stream.forEach(System.out::println); }4、创建无限流 可以使用静态方法 Stream.iterate() 和 Stream.generate(), 创建无限流 public staticT StreamT iterate(final T seed, final UnaryOperatorT f)public staticT StreamT generate(SupplierT s) Test public void test05() {// 迭代累加获取前五个StreamInteger stream Stream.iterate(1, x - x 2);stream.limit(5).forEach(System.out::println);System.out.println(**********************************);// 一直生成随机数获取前五个StreamDouble stream1 Stream.generate(Math::random);stream1.limit(5).forEach(System.out::println); }输出结果 1 3 5 7 9 ********************************** 0.1356905695577818 0.33576714141304886 0.7325647295361851 0.29218866245097375 0.24849848127040652三、Stream中间操作 多个中间操作可以连接起来形成一个流水线除非流水线上触发终止操作否则中间操作不会执行任何的处理而在终止操作时一次性全部处理称为“惰性求值” 准备测试数据 // Data 注在类上提供类的get、set、equals、hashCode、toString等方法 Data AllArgsConstructor NoArgsConstructor public class Employee {// idprivate int id;// 名称private String name;// 年龄private int age;// 工资private double salary; }public class EmployeeData {public static ListEmployee getEmployees(){ListEmployee list new ArrayList();list.add(new Employee(1001, 马化腾, 34, 6000.38));list.add(new Employee(1002, 马云, 2, 19876.12));list.add(new Employee(1003, 刘强东, 33, 3000.82));list.add(new Employee(1004, 雷军, 26, 7657.37));list.add(new Employee(1005, 李彦宏, 65, 5555.32));list.add(new Employee(1006, 比尔盖茨, 42, 9500.43));list.add(new Employee(1007, 任正非, 26, 4333.32));list.add(new Employee(1008, 扎克伯格, 35, 2500.32));return list;} }1、筛选filter 查询员工表中薪资大于7000的员工信息 // 获取员工集合数据 ListEmployee employeeList EmployeeData.getEmployees(); StreamEmployee stream employeeList.stream(); stream.filter(emp - emp.getSalary() 7000).forEach(System.out::println);2、截断limit 获取员工集合数据的前十个员工信息 employeeList.stream().limit(10).forEach(System.out::println);3、跳过skip 返回一个删除前五个员工信息的数据与limit(n)互补 employeeList.stream().skip(5).forEach(System.out::println);4、去重distinct 通过流所生成元素的hashCode()和equals()去除重复元素需要重写hashCode和equals方法否则不能去重 employeeList.add(new Employee(1009, 马斯克, 40, 12500.32)); employeeList.add(new Employee(1009, 马斯克, 40, 12500.32)); employeeList.add(new Employee(1009, 马斯克, 40, 12500.32)); employeeList.add(new Employee(1009, 马斯克, 40, 12500.32));employeeList.stream().distinct().forEach(System.out::println);5、映射map/flatMap/mapToInt map获取员工姓名长度大于3的员工的姓名 //方式1Lambda表达式 employeeList.stream().map(emp - emp.getName()).filter(name - name.length() 3).forEach(System.out::println); //方式2方法引用第三种 类 :: 实例方法名 employeeList.stream().map(Employee::getName).filter(name - name.length() 3).forEach(System.out::println);flatMap当处理嵌套集合时可以使用flatMap将嵌套集合展平成一个新的Stream ListListInteger nestedList Arrays.asList(Arrays.asList(1, 2, 3),Arrays.asList(4, 5, 6),Arrays.asList(7, 8, 9) );nestedList.stream().flatMap(item - item.stream()).forEach(System.out::println);mapToInt将流中每个元素映射为int类型 // 获取名字长度的总和 ListString list1 Arrays.asList(Apple, Banana, Orange, Grapes); IntStream intstream list1.stream().mapToInt(String::length); int sum intstream.sum();mapToDouble将流中每个元素映射为Double类型mapToLong将流中每个元素映射为Long类型 6、排序sorted sorted()自然排序(从小到大)流中元素需实现Comparable接口否则报错 //sorted() 自然排序 Integer[] arr new Integer[]{345,3,64,3,46,7,3,34,65,68}; String[] arr1 new String[]{GG,DD,MM,SS,JJ};Arrays.stream(arr).sorted().forEach(System.out::println); Arrays.stream(arr1).sorted().forEach(System.out::println);//因为Employee没有实现Comparable接口所以报错 employeeList.stream().sorted().forEach(System.out::println);sorted(Comparator com)Comparator排序器自定义排序 // 根据工资自然排序从小到大 employeeList.stream().sorted(Comparator.comparing(Employee::getSalary)).forEach(System.out::println); // 根据工资倒序从大到小 employeeList.stream().sorted(Comparator.comparing(Employee::getSalary).reversed()).forEach(System.out::println);7、peek 和 forEach 相同点peek和forEach都是遍历流内对象并且对对象进行一定的操作不同点forEach返回void结束Stream操作peek会继续返回Stream对象 employeeList.stream().map(Employee::getName).peek(System.out::println).filter(name - name.length() 3).forEach(System.out::println);四、Stream终止操作 终端操作会从流的流水线生成结果。其结果可以是任何不是流的值例如List、Integer甚至是 void流进行了终止操作后不能再次使用 1、匹配allMatch/anyMatch/noneMatch allMatch(Predicate p)检查是否匹配所有元素是否所有的员工的年龄都大于18 boolean allMatch employeeList.stream().allMatch(emp - emp.getAge() 18);anyMatch(Predicate p)检查是否至少匹配一个元素是否存在年龄大于18岁的员工 boolean anyMatch employeeList.stream().anyMatch(emp - emp.getAge() 18);noneMatch(Predicate p)检查是否没有匹配所有元素是不是没有年龄大于18岁的员工没有返回true存在返回false boolean noneMatch employeeList.stream().noneMatch(emp - emp.getAge() 18);2、查找findFirst/findAny findFirst()返回第一个元素 OptionalEmployee first employeeList.stream().findFirst(); Employee employee first.get();findAny()返回当前流中的任意元素 OptionalEmployee any employeeList.stream().findAny(); Employee employee any.get();ps集合中数据为空会抛异常No value present后面会将Optional类的空值处理 3、聚合max/min/count max(Comparator c)返回流中最大值入参与排序sorted的比较器一样必须自然排序返回最高工资的员工 OptionalEmployee max employeeList.stream().max(Comparator.comparingDouble(Employee::getSalary)); Employee employee max.get();min(Comparator c)返回流中最小值入参与排序sorted的比较器一样必须自然排序返回最低工资的员工 OptionalEmployee min employeeList.stream().min(Comparator.comparingDouble(Employee::getSalary)); Employee employee min.get();count()返回流中元素总数返回所有工资大于7000的员工的个数 long count employeeList.stream().filter(emp - emp.getSalary() 7000).count();4、归约reduce reduce(BinaryOperator b)可以将流中元素反复结合起来得到一个值。返回 OptionalT // 计算1-10的自然数的和 ListInteger list Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); OptionalInteger reduce6 list.stream().reduce(Integer::sum); Integer sum reduce6.get();// 计算公司所有员工工资的总和 OptionalDouble reduce7 employeeList.stream().map(Employee::getSalary).reduce(Double::sum); Double aDouble reduce7.get();reduce(T identity, BinaryOperator b)可以将流中元素反复结合起来得到一个值。返回 TT identity累加函数的初始值不需要先获取Optional再get()直接可以获取结果推荐使用 // 计算1-10的自然数的和 ListInteger list Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Integer reduce1 list.stream().reduce(0, (x1, x2) - x1 x2); Integer reduce2 list.stream().reduce(0, (x1, x2) - Integer.sum(x1, x2)); Integer reduce3 list.stream().reduce(0, Integer::sum); // 计算1-10的自然数的乘积 Integer reduce4 list.stream().reduce(1, (x1, x2) - x1 * x2);// 计算公司所有员工工资的总和 Double reduce5 employeeList.stream().map(Employee::getSalary).reduce(0.0, Double::sum);5、收集(collect) collect(Collector c)将流转换为其他形式接收一个Collector接口的实现用于给Stream中元素做汇总的方法Collector接口中方法的实现决定了如何对流执行收集的操作(如收集到 List、Set、Map)Collectors实用类提供了很多静态方法可以方便地创建常见收集器实例如下 5.1、归集toList/toSet/toMap toList()把流中元素收集到List获取所有员工姓名集合 ListString nameList1 employeeList.stream().map(Employee::getName).collect(Collectors.toList()); // jdk16以后collect(Collectors.toList())可以简写为.toList() ListString nameList2 employeeList.stream().map(Employee::getName).toList();toSet()把流中元素收集到Set获取所有员工年龄set集合可以去重 SetInteger ageList employeeList.stream().map(Employee::getAge).collect(Collectors.toSet());toMap()把流中元素收集到MapFunction.identity()t - t相当于参数是什么返回什么 如下如果key重复会抛异常java.lang.IllegalStateException: Duplicate key xxx // key-名称 value-员工对象 MapString, Employee employeeNameMap employeeList.stream().collect(Collectors.toMap(Employee::getName, Function.identity())); // key-名称 value-工资 MapString, Double nameSalaryMap employeeList.stream().collect(Collectors.toMap(Employee::getName, Employee::getSalary));toMap的第三个参数则是key重复后如何操作value的内容key重复可以只要旧的value数据也可以新的value旧的value等等 // key-名称 value-员工对象 MapString, Employee employeeNameMap employeeList.stream().collect(Collectors.toMap(Employee::getName, Function.identity(),(oldValue,newValue) - oldValue)); // key-名称 value-工资 MapString, Double nameSalaryMap employeeList.stream().collect(Collectors.toMap(Employee::getName, Employee::getSalary,(oldValue,newValue) - oldValue newValue));5.2、统计counting/averaging/summing/maxBy/minBy counting()计算流中元素的个数 Long count employeeList.stream().collect(Collectors.counting()); // 相当于 Long count2 employeeList.stream().count();averagingInt计算流中元素Integer属性的平均值averagingDouble计算流中元素Double属性的平均值averagingLong计算流中元素Long属性的平均值返回值都是Double Double aDouble employeeList.stream().collect(Collectors.averagingInt(Employee::getAge)); Double bDouble employeeList.stream().collect(Collectors.averagingDouble(Employee::getSalary));summingInt计算流中元素Integer属性的总和summingDouble计算流中元素Double属性的总和summingLong计算流中元素Long属性的总和 Integer count employeeList.stream().collect(Collectors.summingInt(Employee::getAge)); Double total employeeList.stream().collect(Collectors.summingDouble(Employee::getSalary));maxBy()计算流最大值minBy()计算流最小值 // 最大值 OptionalEmployee employee employeeList.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary))); // 最小值 OptionalEmployee employee employeeList.stream().collect(Collectors.minBy(Comparator.comparingDouble(Employee::getSalary)));summarizingInt()汇总统计包括总条数、总和、平均数、最大值、最小值 IntSummaryStatistics summaryStatistics employeeList.stream().collect(Collectors.summarizingInt(Employee::getAge)); System.out.println(summaryStatistics);// IntSummaryStatistics{count8, sum263, min2, average32.875000, max65}5.3、分组partitioningBy/groupingBy partitioningBy()根据true或false进行分区将员工按薪资是否高于6000分组 MapBoolean, ListEmployee listMap employeeList.stream().collect(Collectors.partitioningBy(emp - emp.getSalary() 6000));groupingBy()根据某属性值对流分组属性为K结果为V将员工年龄分组 MapInteger, ListEmployee collect employeeList.stream().collect(Collectors.groupingBy(Employee::getAge));将员工按年龄分组再汇总不同年龄的总金额 MapInteger, Double collect employeeList.stream().collect(Collectors.groupingBy(Employee::getAge, Collectors.summingDouble(Employee::getSalary)));将员工按年龄分组获取工资集合 MapInteger, ListDouble integerListMap employeeList.stream().collect(Collectors.groupingBy(Employee::getAge, Collectors.mapping(Employee::getSalary, Collectors.toList())));先按员工年龄分组再按工资分组 MapInteger, MapDouble, ListEmployee collect employeeList.stream().collect(Collectors.groupingBy(Employee::getAge, Collectors.groupingBy(Employee::getSalary)));5.4、接合joining ListString list Arrays.asList(A, B, C); String string list.stream().collect(Collectors.joining(-)); // 结果A-B-C五、Optional 类 Optional类内部结构value为实际存储的值 public final class OptionalT {// 空Optional对象value为nullprivate static final Optional? EMPTY new Optional();// 实际存储的内容private final T value;// 私有的构造private Optional() {this.value null;}... }1、构建Optional对象 Test public void optionalTest(){Integer value1 null;Integer value2 10;// 允许传递为null参数OptionalInteger a Optional.ofNullable(value1);// 如果传递的参数是null抛出异常NullPointerExceptionOptionalInteger b Optional.of(value2);// 空对象value为nullOptionalObject c Optional.empty(); }2、获取value值空值的处理 Test public void optionalTest() {Integer value1 null;OptionalInteger a Optional.ofNullable(value1);System.out.println(value值是否为null: a.isPresent());System.out.println(获取value值,空报错空指针: a.get());System.out.println(获取value值,空返回默认值0: a.orElse(0));System.out.println(获取value值,空返回Supplier返回值: a.orElseGet(() - 100));System.out.println(获取value值,空抛出异常: a.orElseThrow(() - new RuntimeException(value为空))); }3、处理value值空值不处理不报错 ifPresent方法内会判断不为空才操作 Test public void optionalTest() {Integer value1 10;OptionalInteger a Optional.ofNullable(value1);// 空不处理非空则根据Consumer消费接口处理a.ifPresent(o - System.out.println(ifPresent value值: o)); // 10// 空不处理filter过滤a.filter(o - o 1).ifPresent(o - System.out.println(filter value值: o)); // 10// 空不处理map映射a.map(o - o 10).ifPresent(o - System.out.println(map value值: o)); // 20// 空不处理flatMap映射a.flatMap(o - Optional.of(o 20)).ifPresent(o - System.out.println(flatMap value值: o)); // 30 }
http://www.w-s-a.com/news/661738/

相关文章:

  • dede 网站模板哈尔滨房产信息网官方网站
  • 设计师个人作品集模板班级优化大师网页版登录
  • 高端网站建设教学网站开发前期准备工作
  • 网站评论列表模板设计官网的
  • 怎么做可以访问网站ui设计自学学的出来吗
  • 网站如何接入支付宝软件开发工作内容描述
  • 廊坊网站建设搭建整合营销传播的效果表现为
  • 网站服务器在本地是指园林绿化
  • 公司网站建设需要什么科目网站代运营价格
  • 网站建设前的ER图ppt模板图片 背景
  • 做一个网站花多少钱网站导航营销步骤
  • 仙桃网站定制做房产网站能赚钱吗
  • 西安网站制作模板最新源码
  • 南京江宁网站建设大学高校网站建设栏目
  • 模板网站建设明细报价表做网站第一
  • 公司网站建设系统软件开发 上海
  • 怎么让公司建设网站固安县建设局网站
  • 360免费建站官网入口手机网站建设设计
  • 商城网站建站系统dw如何做网页
  • 网站建设的公司收费我有网站 怎么做淘宝推广的
  • 网站建设策划书事物选题手机兼职app
  • html5 微网站模版wordpress博客速度很慢
  • 怎么做五个页面网站网络推广如何收费
  • 上虞宇普电器网站建设江西建筑人才网
  • 在吗做网站商城一个网站需要服务器吗
  • 先做网站再备案吗中山微网站建设报价
  • 树莓派可以做网站的服务器吗网站建设与设计ppt
  • 网站访问速度分析网站怎么做让PC和手机自动识别
  • 网站建设要考西宁网站建设多少钱
  • 网站开发公司东莞网站推广计划书具体包含哪些基本内容?