您好,欢迎访问一九零五行业门户网

Java8 Lamdba函数式推导的语法怎么表达

前言有且只有一个抽象函数的接口就是函数式接口,利用函数式接口我们就可以创建lamdba,但是其中可以包括静态方法和default
1,lamdba表达式的语法首先我们来看一个简单的lamdba表达式的应用,就是在创建线程时候
//创建一个线程,将线程的名字打印出来 new thread(()- system.out.println(thread.currentthread().getname())).start(); thread.currentthread().join(); //thread-0
看下面的例子:
comparator<apple> bycolor = new comparator<apple>() { @override public int compare(apple o1, apple o2) { return o1.getcolor().compareto(o2.getcolor()); } }; listapple.sort(bycolor); comparator<apple> bycolor2 = (o1,o2) -> o1.getcolor().compareto(o2.getcolor());
我们不难看到lamdba的语法构成如下:
参数+函数体就像上面的o1 和o2就是参数,后面的就是函数体 (apple a) -> a.getcolor() (parameters)-> expression (parameters) ->{statements}
compareto函数介绍:
// 根据unicode来判断,返回一个boolean 根据boolean来说明,这两个对象需不需要交换,其实我们可以不用这个compareto,我们可以自己写判断条件,只要是返回boolean就行 public int compareto(string anotherstring) { int len1 = value.length; int len2 = anotherstring.value.length; int lim = math.min(len1, len2); char v1[] = value; char v2[] = anotherstring.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
2,常见的函数式接口predicate boolean test(t t)
consumer accept(t t)
function<t, r> r apple(t,t)
supplier t get()
predicate :传入一个变量返回一个boolean值
private static list<apple> filterapple(list<apple> list, predicate<apple> predicate){ list<apple> lists = new arraylist<>(); for (apple apple:list){ if (predicate.test(apple)){ lists.add(apple); } } return lists; }//使用 list<apple> list = filterapple(listapple, apple -> apple.getweight() > 100); system.out.println(list);//我们发现在 filterapple 函数里面我们传入了两个参数,一个是list 一个是predicate抽象函数式接口,我们调用这个抽象函数式接口,但是我们不实现它,我们在调用的时候实现它是根据重量来比较,当然我们在调用的时候也可以根据颜色来比较/**举一反三:predicate是传入一个参数进行判断,bipredicate是传入两个参数进行判断,例如下面的例子*/private static list<apple> filterapplebybipredicate(list<apple> apples, bipredicate<string, long> predicate){list<apple> lists = new arraylist()for(apple apple:apples){ if (predicate.test(apple.getcolor(),apple.getweight())){ lists.add(apple) }}//从上面的例子上来看我们知道bipredicate传入两个参数,根据颜色和重量来进行比较,我们在调用的时候可以实现者两个参数的比较规则}
consumer 这个单词中文意思是消费者,既然消费了就不会有返回值
private static void printlnapple(list<apple> list, consumer<apple> consumer){ if (list.size()>0){ for (apple apple:list){ consumer.accept(apple); } } }//使用printlnapple(listapple,apple -> system.out.println(apple.getcolor()));/**从上面我们可以看到 这个consumer不会返回任何对象举一反三: 上面有bipredicate,那么我们这个consumer有没有biconsumer,答案肯定是有的,下面我们来看下biconsumer的用法*/private static void printgreenapplebybiconsumer(list<apple> apples, consumer<apple, string> consumer){ if(apples.size()>0){ for(apple apple:apples){ consumer.accept(apple, apple.getcolor()) } }}//使用printgreenapplebybiconsumer(list,(apple, color)->{ if(apple.getweight()>100&&color.equal("green")){ system.out.println(apple); }})//学习玩上面的例子 我再提一个 longconsumer,从字面上看,我们知道,它接收的是一个long类型的,其他的并没什么区别,这里我们就不多啰嗦了
function:函数,我们知道函数都是有输出的,这里的意思也就是说传入一个参数,返回一个参数,下面我们来看下怎么使用这个function
private static long getweightlist(apple apple, function<apple,long> function) { return function.apply(apple); }//使用 long green = getweightlist(new apple("green", 120), apple::getweight); system.out.println(green);//在这里我们可以看到,function函数的apply抽象方法接收一个值,返回一个long类型的值//根据上面的经验,想必大家也已经知道了还有 bifunction 这个函数式接口,没错它只是传进去的参数有两个而已,还是返回一个结果。除此之外还有呢,大家别着急,还有 intfunction doublefunction,这些有什么作用呢,其实可以看成是function的具体情况 intfuntion也就是 apply里面的参数是int类型的 doublefunction也就是apply的参数是double类型的,大家再看一下,是不是非常非常的简单呢
supplier:这个单词的意思是提供的意思,那也就是给你返回数据,这个函数接口里面会给你返回数据,不需要你传入数据
private static apple getapple(supplier<apple> supplier){ return supplier.get(); } //使用 apple green1 = getapple(() -> new apple("green", 150)); system.out.println(green1); //有人肯定再想了,这样创建对象那不是脱裤子放屁吗 多此一举,这里只是举一个小例子,在实际开发中肯定不会这样
3,lamdba表达式之函数式推导1,通过一个类的静态方法去推断
2,用对象的实例方法去推断
private static void printstr(consumer<string> consumer, string str){ consumer.accept(str); } //使用 consumer<string> consumer = a -> system.out.println(a); printstr(consumer,"hello world"); //写成 consumer<string> consumer = system.out::println printstr(consumer,"hello world"); //思考我们为什么可以写成这样呢,我们看下println的源码 // public final static printstream out = null;,在源码里面我们可以看到printstream是一个静态的类,而println是这个静态类下面的方法,因此可以做函数推导 public void println(string x) { //刚好是接收一个参数,没有返回 synchronized (this) { print(x); newline(); } }```再举个例子```java//将字符串转换成数字int value = integer.parseint("123") function<string, integer> stringfunction = integer::parseint; integer result = stringfunction.apply("123"); system.out.println(result) /** 很多人看到这里就很纳闷了,怎么冒出一个 function来了呢,这就是类方法推导,parseint是integer的类方法它需要传入一个数,然后输出一个数,这样的模式就是前面我们介绍的function函数式接口,接着再来看一个例子 */ string string = new string("hello"); function<integer, character> f = string::charat; character c = f3.apply(4); system.out.println(c);
构造函数推导:
supplier<string> supplier = string::newstring s = supplier.get();system.out.print(s)//但是apple是两个构造参数的,怎么办呢,还是有办法的bifunction<string, long,apple> applefunction = apple::new;apple apple = applefunction.apply("red",100l);//如果有更多的参数呢,在这里我们就可以自定义函数式接口public interface cmyfuction<t,u,w,r>{ r get(t t,u u, w w,r,r);}cmyfuction<string,string, long> cmyfunction = apple::new;apple apple = cmyfunction.get("green","large",120);//ok 这样自定义的函数式接口就完成了
例子:sort分类与函数式推导
//定义一个集合 static list<apple> listapple = arrays.aslist( new apple("green",122), new apple("red", 34), new apple("black",135), new apple("green",114), new apple("yellow",54), new apple("yellow",94)); //以前我们排序的时候只知道调用 sort方法例如 listaplle.sort() //但是我们不知道它怎么操作的,下面我们可以自定义排序的规则 list.sort() //写道这里系统提示参数可以为comparator,我们接着写 list.sort(new comparator<apple>{ public int compare(apple o1, apple o2){ return o1.getcolor().compareto(o2.getcolor()); }}) //从上面的里例子来看我们是根据apple的颜色来进行比较的,当然我们也可以根据apple的weight来进行比较.接着我们进行函数推导 list.sort(comparator.comparing(apple::getcolor)) //就这么一句完全代替了我们上面的功能,很明显简化了代码
以上就是java8 lamdba函数式推导的语法怎么表达的详细内容。
其它类似信息

推荐信息