Lambda表达式针对于函数式接口(functionInterface)
(java 1.8适用)
函数式接口的意思就是说 接口里面只有一个抽象方法
Lambda表达式是java代码变得很简单,但是这样可读性就会变的很差劲;
Lambda表达式很重要的一点:
它是根据接口式函数来写的
接口式函数给了参数 Lambda表的是的小括号里() 就给参数
没有给参数 ()小括号里可以什么也不用写
interface Intfo{
void intfo();
}
public class TestLambda {
public static void main(String[] args) {
new Intfo() {
@Override
public void intfo() {
}
};
}
}
//----------------------------
interface Intfo {
void intfo();
}
public class TestLambda {
Intfo intfo = new Intfo() {
@Override
public void intfo() {
}
};
public static void main(String[] args) {
}
}
例子当中定义了一个接口Intfo
只有一个抽象方法
当然也可以拥有默认方法或者是静态方法
但是抽象方法只能有一个才能使用Lambda表达式
这是以匿名内部类两种不同方式展示的(匿名内部类式Lambda表达式的前提)
interface Intfo {
void intfo();
}
public class TestLambda {
Intfo intfo =()->{};
public static void main(String[] args) {
}
}
只有一行代码,一样的可以实现匿名内部类实现的功能
interface Intfo {
int intfo(int i);
}
public class TestLambda {
Intfo intfo = (int i)->i;
public static void main(String[] args) {
}
}
//-------------------------------------
interface Intfo {
int intfo(int i);
}
public class TestLambda {
Intfo intfo = (i)->i;
public static void main(String[] args) {
}
}
定义了一个有参数并且有返回值的接口式函数
当代码只有一行的时候
"{}"这一对大括号 可以不要
return 也可以不用要
甚至连参数的类型也不需要
方法的引用:
方法的引用可以是Lambda表达式的形式更简单
1)
interface Intfo {
Integer intfo(String s);
}
public class TestLambda {
public static void main(String[] args) {
Intfo intfo = (s)->Integer.parseInt(s);
}
}
调用Interger的类方法 将字符串S转换成Interger类型
interface Intfo {
Integer intfo(String s);
}
public class TestLambda {
public static void main(String[] args) {
// Intfo intfo = (s)->Integer.parseInt(s);
Intfo intfo =Integer::parseInt;
}
}
引用类方法:
类名::方法名(中间是两个冒号::)
2)
interface Intfo {
Integer intfo(String s);
}
public class TestLambda {
public static void main(String[] args) {
Intfo intfo = (s)->"hello".indexOf(s);
}
}
"hello"表示的是一个特定的对象
interface Intfo {
Integer intfo(String s);
}
public class TestLambda {
public static void main(String[] args) {
// Intfo intfo = (s)->"hello".indexOf(s);
Intfo intfo = "hello"::indexOf;
}
}
引用某个特定对象的实例方法:
特定类型::实例方法名
3)
interface Intfo {
Integer intfo(String s);
}
public class TestLambda {
public static void main(String[] args) {
Intfo intfo = (s)->s.length();
}
}
字符串S 返回S的长度
interface Intfo {
Integer intfo(String s);
}
public class TestLambda {
public static void main(String[] args) {
// Intfo intfo = (s)->s.length();
Intfo intfo = String::length;
}
}
引用某类对象的实例方法:
类名::方法名
4)
class Student{
String name;
Student(String name){
this.name=name;
}
}
interface Intfo {
Student intfo(String name);
}
public class TestLambda {
public static void main(String[] args) {
Intfo intfo = (name)->new Student(name);
}
}
创建了一个Student类
有一个有参的构造函数
接口Intfo的intfo()方法返回一个Student的对象
class Student{
String name;
Student(String name){
this.name=name;
}
}
interface Intfo {
Student intfo(String name);
}
public class TestLambda {
public static void main(String[] args) {
// Intfo intfo =(name)->new Student(name);
Intfo intfo =Student::new;
}
}
引用构造方法:
类名::new
简单的整理了一下Lambda表达式的笔记~
Lambda表达式看起来逼格很高 但是可读性很low
努力努力再努力~