如何选择赣州网站建设,电脑培训速成班多少钱,wordpress 学术主题,wordpress 页面 置顶lambda表达式
什么是lambda
学习lamdba有两个结构十分关键#xff0c;一个是lamdba自己#xff0c;另一个是函数式接口
lamdba
lamdba表达式本质上就是匿名方法#xff0c;不能独立运行用于实现函数式接口定义的另一个方法#xff0c;因此lamdba会产生一个匿名类lamdba…lambda表达式
什么是lambda
学习lamdba有两个结构十分关键一个是lamdba自己另一个是函数式接口
lamdba
lamdba表达式本质上就是匿名方法不能独立运行用于实现函数式接口定义的另一个方法因此lamdba会产生一个匿名类lamdba也常被称作闭包引入了新的语法操作符 -
函数式接口
只包含一个抽象方法的接口这个方法说明了这个接口的意义和使用函数式接口通常表示单个动作有时被称作SAM类型单抽象方法
lambda表达式示例
interface MyNumber{double getValue();
}public static void main(String[] args){MyNumber myNum;myNum ()- 3.1415;System.out.println(myNum.getValue())
}//输出
3.1415块lambda
处理简单的赋值等操作可以使用单个表达式处理复杂的语句可以使用块lambda,可以使用多条语句最后必须显示的使用return 返回值
块lambda 阶乘示例
interface MyNum{int func(int n);
}public void test1(){MyNum mm (n)-{int result 1;for(int i1;in;i){result * i;}return result;};System.out.println(mm.func(4));}泛型函数式接口
示例
interface SomeFuncT{T func(T t);
}public void test2(){SomeFuncInteger num (n)- n*10;SomeFuncString str (s)- hello s;System.out.println(num.func(10));System.out.println(str.func(tom));}当作参数传递
interface SomeFuncT{T func(T t);
}public String getResult(SomeFuncString some,String str){return some.func(str);}public void test3(){String str hello java for lambda;String ret getResult((n)-{return n.toUpperCase();},str);System.out.println(ret);String ret1 getResult((n)-{return new StringBuffer(str).reverse().toString();},str);System.out.println(ret1);}
//输出
HELLO JAVA FOR LAMBDA
adbmal rof avaj ollehlambda 表达式与异常
lambda可以抛出异常异常必须和函数式接口throws抛出的异常一致
代码示例
interface DoubleNumberFun{double fun(double[] n)throws DoubleNumberException;
}class DoubleNumberException extends Exception{public DoubleNumberException(){super(array empty);}
}public void test4() throws DoubleNumberException {DoubleNumberFun df (n)-{if(n.length 0)throw new DoubleNumberException();double sm 0.0;for(int i0;in.length;i){sm n[i];}return sm;};System.out.println(df.fun(new double[]{1.0,2.0,3.0}));System.out.println(df.fun(new double[]{}));}
//输出//6.0
//com.bai.lambda.DoubleNumberException: array emptylambda 表达式变量捕获
lambda 中可以显示或者隐式的使用this使用局部变量默认是final 修饰不能改变不管有没有使用final。否则会提示Variable used in lambda expression should be final or effectively final
代码示例
interface MyNum{int func(int n);
}int variable 10;public void test5(){int variable1 0;MyNum m (n)-{int sum 0;for(int i0;in;i){sum i variable1;}//此处报错//Variable used in lambda expression should be final or effectively final//variable1 sum;return sum;};MyNum m1 (n)-{int sum 0;for(int i0;in;i){//Variable used in lambda expression should be final or effectively finalsum i this.variable;}this.variable sum;return sum;};}方法引用
静态方法引用
形式 classname::methodName:: 是jdk8新添加分隔符专门用于此处函数式接口的方法要和静态方法兼容比如下面的func 方法和strReverse兼容
代码示例
函数式接口
interface StringFunc{String func(String str);
}静态方法 public static String StrReverse(String str){char[] chars new char[str.length()];for(int i0;istr.length();i){chars[i] str.charAt(str.length()-1-i);}return new String(chars);}方法引用
public static String stringOpts(StringFunc func,String str){return func.func(str);}测试方法 public static void main(String[] args) {String result stringOpts(MyStringOpts::StrReverse,helllo lambda static);System.out.println(result);}
//输出
//citats adbmal ollleh实例中使用
对象调用 objRef::methodname 需要先 new 对象在进行调用 interface StringFunc{String func(String str);
}class ObjRef{public String reverse(String str){StringBuffer buf new StringBuffer();for(int i0;istr.length();i){buf.append(str.charAt(str.length()-1-i));}return buf.toString();}}class MainTest1{public static String stringOpts(StringFunc func,String str){return func.func(str);}public static void main(String[] args) {ObjRef objRef new ObjRef();String result stringOpts(objRef::reverse,你好 lambda);System.out.println(result);}
}
//输出
//adbmal 好你实例方法 className::methodName 第一个参数匹配调用对象第二个参数匹配调用方法的参数
具体请看代码示例
函数式接口
注意这个函数有两个参数
interface MyFuncT{boolean func(T t1,T t2);
}实体对象
主要使用下面的equalslessThan方法两个方法中的参数对应函数式接口中的第二个参数
class Student{private Integer glades;public Student(Integer glades){this.glades glades;}public boolean equals(Student student){return glades student.glades;}public boolean lessThan(Student student){return glades student.glades;}}方法引用 public static T int count(T[] val,MyFuncT f,T v){int count 0;for(int i0;ival.length;i){if(f.func(val[i],v))count;}return count;}测试 Student[] arrs new Student[]{new Student(50),new Student(60),new Student(60),new Student(80),new Student(90)};int ret count(arrs,Student::equals,new Student(60));System.out.println(ret);
//2int ret1 count(arrs,Student::lessThan,new Student(60));System.out.println(ret1);
//2泛型中引用
接口
interface MyFuncT{int func(T[] vals,T t);
}泛型方法非泛型类
public class FanxingLambda {static T int countMath(T[] vals,T t){int count 0;for(int i0;ivals.length;i){if(vals[i] t)count;}return count;}
}引用方法 public static T int countFun(MyFuncT f,T[] vals,T t){return f.func(vals,t);}测试方法 public static void main(String[] args) {Integer[] arrs new Integer[]{1,2,3,4,5,2,4,5,2,1,2,3};int count countFun(FanxingLambda::IntegercountMath,arrs,2);System.out.println(count);}注意
int count countFun(FanxingLambda::IntegercountMath,arrs,2);FanxingLambda::countMath
因为FanxingLambda不是泛型类但是countMath是泛型方法所以可以在::前面指定Integer类型
也可以不写因为类型参数会推断出类型
找出最大值示例
需要使用Collections.max方法其中第二个参数传入一个比较器Comparator
源码 Collections :
public static T T max(Collection? extends T coll, Comparator? super T comp) Comparator:
int compare(T o1, T o2);测试代码
实体
class Salary{private double val;public Salary(double val){this.val val;}public double getVal(){return val;}public static int comparaSalary(Salary s1,Salary s2){return (int)(s1.val - s2.val);}
}测试方法 public static void main(String[] args) {ListSalary list new ArrayList();list.add(new Salary(100.1));list.add(new Salary(2000.3));list.add(new Salary(1000));list.add(new Salary(3000.5));list.add(new Salary(235));Salary s Collections.max(list,Salary::comparaSalary);System.out.println(s.getVal());}//3000.5构造函数引用
classname::new
interface ClassFunT,V{T func(V v);
}class Emp{private Integer age;public Emp(Integer age){this.age age;}public Integer getAge(){return age;}
}class PeopleT{private T t;public People(T t){this.t t;}public T getVal(){return t;}
}ClassFunEmp,Integer c Emp::new;Emp e c.func(100);System.out.println(e.getAge());ClassFunPeopleString,String c1 People::new;PeopleString p c1.func(zhangsan);System.out.println(p.getVal());//100//zhangsan
文章转载自: http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.cwgpl.cn.gov.cn.cwgpl.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.jynzb.cn.gov.cn.jynzb.cn http://www.morning.mlnbd.cn.gov.cn.mlnbd.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn