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

中国互联网金融协会官网奇零seo赚钱培训

中国互联网金融协会官网,奇零seo赚钱培训,物流公司网站建设,网站建设那个公司好lambda与stream是java8中比较重要两个新特性#xff0c;lambda表达式采用一种简洁的语法定义代码块#xff0c;允许我们将行为传递到函数中。之前我们想将行为传递到函数中#xff0c;仅有的选择是使用匿名内部类#xff0c;现在我们可以使用lambda表达式替代匿名内部类。在…lambda与stream是java8中比较重要两个新特性lambda表达式采用一种简洁的语法定义代码块允许我们将行为传递到函数中。之前我们想将行为传递到函数中仅有的选择是使用匿名内部类现在我们可以使用lambda表达式替代匿名内部类。在学习lambda表达式之前建议各位看官先去学习一下匿名内部类JAVA基础知识|内部类。 stream提供了很多有用的api方便了我们对集合的操作 一、lambda表达式 基本语法:(parameters) - expression或(parameters) -{ statements; }   lambda表达式的重要特征: 可选类型声明不需要声明参数类型编译器可以统一识别参数值。可选的参数圆括号一个参数无需定义圆括号但多个参数需要定义圆括号。可选的大括号如果主体包含了一个语句就不需要使用大括号。可选的返回关键字如果主体只有一个表达式返回值则编译器会自动返回值大括号需要指定明表达式返回了一个数值。 以下是一些简单的例子便于我们理解这些特性 // 1. 不需要参数,返回值为5() - 5// 2. 接收一个参数,()可以省略,返回其2倍的值x - 2 * x// 3. 接受2个参数(数字),并返回他们的差值(x, y) -x –y// 4. 接收2个int型整数,返回他们的和( int x, int y) -x y// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)(String s) -System.out.print(s) 二、lambda表达式使用 2.1、在for循环中使用 package com.my.controller;import junit.framework.TestCase; import org.junit.Test;import java.util.ArrayList; import java.util.List;/*** description:{description}* author:jyy* date:2018-01-09 16:43* modify:{modify}*/ public class AppTest extends TestCase {Testpublic void test() {ListString list new ArrayList();list.add(北京);list.add(上海);list.add(广州);list.add(深圳);for (String str : list) {System.out.println(str);}System.out.println();list.forEach(str - System.out.println(str));}} 执行结果 北京 上海 广州 深圳北京 上海 广州 深圳 2.2、替代匿名内部类使用 我们使用JAVA基础知识|内部类中的try catch示例 package com.my.controller;import junit.framework.TestCase; import org.junit.Test;import java.util.ArrayList; import java.util.List;/*** description:{description}* author:jyy* date:2018-01-09 16:43* modify:{modify}*/ public class AppTest extends TestCase {Testpublic void test() {//匿名内部类new ExceptionTemplate().execute(new CatchExceptionable() {Overridepublic void catchException() {System.out.println(代码);}});//lambda表达式new ExceptionTemplate().execute(() - System.out.println(代码));}} 2.3、lambda表达式与Comparator类结合使用 package com.my.controller;import junit.framework.TestCase; import org.junit.Test;import java.util.ArrayList; import java.util.Comparator; import java.util.List;/*** description:{description}* author:jyy* date:2018-01-09 16:43* modify:{modify}*/ public class AppTest extends TestCase {Testpublic void test() {ListString list new ArrayList();list.add(BeiJing);list.add(ShangHai);list.add(GuangZhou);list.add(ShenZhen);list.sort(new ComparatorString() {Overridepublic int compare(String o1, String o2) {return o1.compareTo(o2);}});list.forEach(str - System.out.println(str));System.out.println();ListString list2 new ArrayList();list2.add(BeiJing);list2.add(ShangHai);list2.add(GuangZhou);list2.add(ShenZhen);//list2.sort((String o1,String o2) - o1.compareTo(o2));ComparatorString comparator (String o1,String o2) - o1.compareTo(o2);list2.sort(comparator);list2.forEach(str - System.out.println(str));} } 执行结果 BeiJing GuangZhou ShangHai ShenZhenBeiJing GuangZhou ShangHai ShenZhen  三、流stream Java 8 中的stream 是对集合Collection对象功能的增强它专注于对集合对象进行各种非常便利、高效的聚合操作aggregate operation或者大批量数据操作 (bulk data operation)。stream API 借助于同样新出现的 lambda 表达式极大的提高编程效率和程序可读性。同时它提供串行和并行两种模式进行汇聚操作并发模式能够充分利用多核处理器的优势 生成流的两种方式 stream() − 为集合创建串行流。parallelStream() − 为集合创建并行流。下面我们就来使用stream提供的各种API 3.1、筛选和切片 方法描述filter从流中过滤元素distinct通过流所生成的元素的hashCode()和equals()方法去重limit截断流选取前n个元素skip跳过元素返回一个扔掉了前 n 个元素的流。若流中元素不足n个则返回一个空流  package com.my.po;/*** description:{description}* author:jyy* date:2018-02-11 11:06* modify:{modify}*/ public class Employee {private String id;private String name;private double salary;private String sex;public Employee(String id, String name, double salary,String sex) {this.id id;this.name name;this.salary salary;this.sexsex;}public String getId() {return id;}public void setId(String id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}Overridepublic String toString() {return Employee{ id id \ , name name \ , salary salary , sex sex \ };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Employee employee (Employee) o;return id.equals(employee.id);}Overridepublic int hashCode() {return id.hashCode();} } View Code package com.my.controller;import com.my.po.Employee; import junit.framework.TestCase; import org.junit.Test;import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects;/*** description:{description}* author:jyy* date:2018-01-09 16:43* modify:{modify}*/ public class AppTest extends TestCase {Testpublic void test() {Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1004, 谢楠, 11000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);//forEach,查询所有数据list.stream().forEach(e - System.out.println(e.toString()));//filter,查询集合中所有的女性list.stream().filter(e - Objects.equals(e.getSex(), female)).forEach(e - System.out.println(e.toString()));//查询集合中薪资大于6000的雇员list.stream().filter(e - e.getSalary() 7000).forEach(e - System.out.println(e));//limit,查询前两条数据list.stream().limit(2).forEach(e - System.out.println(e.toString()));//distinct,去重,利用Employee对象中的hashCode()和equals()方法list.stream().distinct().forEach(e - System.out.println(e));//skip,跳过前两个list.stream().skip(2).forEach(e - System.out.println(e));} } View Code 3.2、映射 方法描述 map(Function f) 接受一个函数作为参数并将函数应用到每一个元素上返回新的元素 mapToDouble(ToDoubleFunction f) 返回的新元素为double类型 mapToInt(ToIntFunction f) 返回的新元素为int类型 mapToLong(ToLongFunction f) 返回的新元素为long类型 flatMap(Function f) 操作多层嵌套的流使其扁平化 3.2.1、map Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1004, 谢楠, 11000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);//maplist.stream().map(e - e.getSalary()).forEach(e - System.out.println(e)); 执行结果 5000.0 7000.0 10000.0 11000.0 11000.0 可以看出集合list经过map操作之后类型已经改变。具体什么类型由返回值决定 3.2.2、mapToDouble、mapToInt、mapToLong Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1004, 谢楠, 11000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);list.stream().mapToDouble(e - e.getSalary()).forEach(e - System.out.println(e)); 执行结果 5000.0 7000.0 10000.0 11000.0 11000.0 3.2.3、flatMap Employee[] employees1 {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1004, 谢楠, 11000, female)};Employee[] employees2 {new Employee(1005, Marry, 5000, male),new Employee(1006, Linda, 7000, male),new Employee(1007, Cris, 10000, female)};ListEmployee[] list new ArrayList();list.add(employees1);list.add(employees2);list.stream().flatMap(e - Arrays.stream(e)).forEach(e - System.out.println(e.toString())); 执行结果 Employee{id1001, name李明, salary5000.0, sexmale} Employee{id1002, name王明, salary7000.0, sexmale} Employee{id1003, name张丽, salary10000.0, sexfemale} Employee{id1004, name谢楠, salary11000.0, sexfemale} Employee{id1005, nameMarry, salary5000.0, sexmale} Employee{id1006, nameLinda, salary7000.0, sexmale} Employee{id1007, nameCris, salary10000.0, sexfemale}  3.3、排序 方法描述sorted()产生一个新流其中按自然顺序排序sorted(Comparator comp)产生一个新流其中按比较器顺序排序 Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1005, Marry, 6000, male),new Employee(1006, Linda, 9000, male),new Employee(1007, Cris, 10000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);list.stream().map(e - e.getSalary()).sorted().forEach(e - System.out.println(e.toString()));System.out.println();ComparatorString comparator (String o1, String o2) - o1.compareTo(o2);list.stream().map(e - e.getId()).sorted(comparator).forEach(e - System.out.println(e.toString())); 执行结果 5000.0 6000.0 7000.0 9000.0 10000.0 10000.0 11000.01001 1002 1003 1004 1005 1006 1007 3.4、查找与匹配 方法描述allMatch(Predicate p)检查是否匹配所有元素anyMatch(Predicate p)检查是否至少匹配一个元素noneMatch(Predicate p)检查是否没有匹配所有元素findFirst()返回第一个元素findAny()返回当前流中的任意元素count()返回流中元素总数max(Comparator c)返回流中最大值min(Comparator c)返回流中最小值forEach(Consumer c)内部迭代(使用 Collection 接口需要用户去做迭代称为外部迭代。相反Stream API 使用内部迭代——它帮你把迭代做了) Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1005, Marry, 6000, male),new Employee(1006, Linda, 9000, male),new Employee(1007, Cris, 10000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);System.out.println(allMatch);Boolean b1 list.stream().allMatch(e - e.getSalary() 4000);Boolean b2 list.stream().allMatch(e - e.getSalary() 5000);System.out.println(b1);System.out.println(b2);System.out.println(anyMatch);Boolean b3 list.stream().anyMatch(e - e.getSalary() 10000);Boolean b4 list.stream().anyMatch(e - e.getSalary() 11000);System.out.println(b3);System.out.println(b4);System.out.println(noneMatch);Boolean b5 list.stream().noneMatch(e - e.getSalary() 10000);Boolean b6 list.stream().noneMatch(e - e.getSalary() 11000);System.out.println(b5);System.out.println(b6);System.out.println(findFirst);System.out.println(list.stream().findFirst().toString());System.out.println(findAny);System.out.println(list.stream().findAny().toString());System.out.println(count);System.out.println(list.stream().count());System.out.println(max);System.out.println(list.stream().max((Employee o1, Employee o2) - {if (o1.getSalary() o2.getSalary()) return 1;else return -1;}).toString());System.out.println(min);System.out.println(list.stream().min((Employee o1, Employee o2) - {if (o1.getSalary() o2.getSalary()) return 1;else return -1;}).toString()); 执行结果 allMatch true false anyMatch true false noneMatch false true findFirst Optional[Employee{id1001, name李明, salary5000.0, sexmale}] findAny Optional[Employee{id1001, name李明, salary5000.0, sexmale}] count 7 max Optional[Employee{id1004, name谢楠, salary11000.0, sexfemale}] min Optional[Employee{id1001, name李明, salary5000.0, sexmale}]  3.5、归约 map-reduce模式在mongoDB、spark、hadoop等都有它的身影因google使用它进行网络搜索而出名 方法描述reduce(T iden, BinaryOperator b)可以将流中元素反复结合起来得到一个值。返回Treduce(BinaryOperator b)可以将流中元素反复结合起来得到一个值。返回OptionalT Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1005, Marry, 6000, male),new Employee(1006, Linda, 9000, male),new Employee(1007, Cris, 10000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);String result1 list.stream().map(e - e.getName()).reduce(, (x, y) - x , y);System.out.println(result1.substring(1));OptionalString result2 list.stream().map(e - e.getName()).reduce((x, y) - x , y);System.out.println(result2.get()); 执行结果 李明,王明,张丽,Marry,Linda,Cris,谢楠 李明,王明,张丽,Marry,Linda,Cris,谢楠 3.6、收集 方法描述collect(Collector c)将流转换为其他形式。接收一个Collector接口的实现用于给Stream中元素做汇总的方法 Employee[] employees {new Employee(1001, 李明, 5000, male),new Employee(1002, 王明, 7000, male),new Employee(1003, 张丽, 10000, female),new Employee(1005, Marry, 6000, male),new Employee(1006, Linda, 9000, male),new Employee(1007, Cris, 10000, female),new Employee(1004, 谢楠, 11000, female)};ListEmployee list Arrays.asList(employees);ListString collect list.stream().map(e - e.getName()).collect(Collectors.toList());collect.forEach(System.out::println);System.out.println(-------------------);SetString set list.stream().map(e - e.getName()).collect(Collectors.toSet());set.forEach(System.out::println);System.out.println(-------------------);HashSetString hashSet list.stream().map(e - e.getName()).collect(Collectors.toCollection(HashSet::new));hashSet.forEach(System.out::println);System.out.println(-------------------);//分组MapDouble, ListEmployee group list.stream().collect(Collectors.groupingBy(e - e.getSalary()));System.out.println(group); 执行结果 李明 王明 张丽 Marry Linda Cris 谢楠 ------------------- 张丽 Cris 李明 王明 Linda Marry 谢楠 ------------------- 张丽 Cris 李明 王明 Linda Marry 谢楠 {9000.0[Employee{id1006, nameLinda, salary9000.0, sexmale}], 10000.0[Employee{id1003, name张丽, salary10000.0, sexfemale}, Employee{id1007, nameCris, salary10000.0, sexfemale}], 5000.0[Employee{id1001, name李明, salary5000.0, sexmale}], 11000.0[Employee{id1004, name谢楠, salary11000.0, sexfemale}], 6000.0[Employee{id1005, nameMarry, salary6000.0, sexmale}], 7000.0[Employee{id1002, name王明, salary7000.0, sexmale}]} 以上内容只是对lambda表达式和stream的简单介绍在实际使用中要复杂的多  转载于:https://www.cnblogs.com/maikucha/p/8435906.html
http://www.huolong8.cn/news/129323/

相关文章:

  • 网站建设写程序用什么软件广西茶叶网站建设
  • 泉州建设银行网站陕西省建设信息管理网站
  • 房产网站建设产品电商网站用什么做的
  • 做图素材网站哪个好丽江最新防疫政策
  • 赵县住房和城乡建设局网站首页域名查询入口
  • 江门网站开发公司网站开发属于什么软件
  • 做考试平台的网站全免费建立自己的网站
  • 建设银行网站会员基本信息wordpress默认排序
  • 外包网站推荐wordpress dux5.1
  • 电子商务网站 功能房产局官网查询系统
  • 网站模板移植想开网站建设公司
  • 怎么做电影网站吗做资金盘网站
  • 网站包括什么第一次装wordpress
  • 网站内容包括哪些建设全球购多用户商城网站
  • 有一个wordpress站点dw怎么制作好看的个人网页
  • 怎样建一个免费网站广州app开发费用
  • 定制制作网站设计如何让商家建设网站
  • 网站做的比较好的公司吗做游戏网站的分析
  • 教做发型的网站杭州关键词优化外包
  • 网站开发技术与功能 需求说明电脑网站搜索如何做
  • 做自己的外贸网站怎样赚钱php网站制作 青岛
  • 做网站大概需要多少费用北京网站制作哪家好
  • 广西翔路建设有限责任公司网站wordpress主题屋
  • 做电影网站需要注意事项怎么用网站做地标
  • 网站策划岗位职责做网站一般长宽多少钱
  • 网站开发的技术可行性清洁设备网站模版
  • 中江县规划和建设局网站网站添加 备案
  • php网站有哪些广州网站备案号
  • 传媒网站后台免费模板茶文化网站建设内容
  • 绵阳网站建设 科雨网络电子网站怎么做的