背景介绍
- Lambda是JAVA SE8中的一个重要的新特性,允许使用表达式来代替功能接口,提供了一个正常的参数列表和一个使用这些参数的主题(可以是一个表达式或者一个代码块).例如代替匿名内部类,Runnable.
基本语法:
(parameters)->expression /*表达式*/
(parameters)->{statements} /*代码块*/
/-------简单示例------------/
()->a //不需要参数,返回a
a->2*a //接受一个参数(int类型),返回其与2的乘积,一个参数已不写括号
(x, y) ->x-y //接受x,y参数,返回两者的差,虽然编译器会去自动推导类型,但最好还是自己将类型加上
- 简单例子,Lambda表达式代替new 匿名内部类:
public interface TestLambda {
void test(int x);
}
public class TestLambdaImpl {
public void func(TestLambda test) {
test.test(10);
}
}
public void testFunc() {
TestLambdaImpl t = new TestLambdaImpl();
//普通匿名内部类写法
t.func(new TestLambda() {
@Override
public void test(int x) {
System.out.println(x);
}
});
//Lambda表达式,实现了接口的方法,变得更加简洁,等于在在此处就实现了接口的方法
t.func((int x)->System.out.println(x));//表达式
t.func((int x)->{
System.out.println(x);
System.out.println("I am your father!");});//代码块
}
String[] atp = {"Rafael Nadal", "Novak Djokovic",
"Stanislas Wawrinka",
"David Ferrer","Roger Federer",
"Andy Murray","Tomas Berdych",
"Juan Martin Del Potro"};
List list = Arrays.asList(atp);
//以前的循环方式
for(String player : list) {
System.out.println(player);
}
//Lambda表达式
list.forEach((String s)->System.out.println(s));
//使用匿名内部类
new Thread(newRunnbale() {
@Override
public void run() {
System.out.println("hello world");
}
});
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("hello world");
}
};
//Lambda表达式
new Thread(()->System.out.println("hello world"));
Runnable r2 = ()->System.out.println("hello world");
new Thread(r2);
String[] players = {"Rafael Nadal", "Novak Djokovic",
"Stanislas Wawrinka",
"David Ferrer","Roger Federer",
"Andy Murray","Tomas Berdych",
"Juan Martin Del Potro"};
//匿名内部类排序
Arrays.sort(players, new Comparator() {
@Override
public int compare(String s1, String s2) {
return (s1.compareTo(s2));//从大到小的排序
}
});
//Lambda表达式
Comparator comparator = (String s1, String s2) -> s1.compareTo(s2);
Arrays.sort(player, comparator);
Arrays.sort(players, (String s1, String s2) -> return s1.compareTo(s2));
使用Lambdas和Streams
- Stream是对集合的包装,通常与Lambda一起使用,支持多种操作,如 map, filter, limit, sorted, count, min, max, sum, collect 等等。
- 代码
public class Person {
private String firstName, lastName, job, gender;
private int salary, age;
public Person(String firstName, String lastName, String job, String gender, int age, int salary) {
this.firstName = firstName;
this.lastName = lastName;
this.job = job;
this.gender = gender;
this.age = age;
this.salary = salary;
}
}
List javaProgrammers = new ArrayList() {
{
add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 43, 2000));
add(new Person("Tamsen", "Brittany", "Java programmer", "female", 23, 1500));
add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1800));
add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600));
add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200));
add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900));
add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300));
add(new Person("Jayden", "Corrina", "Java programmer", "female", 35, 1700));
add(new Person("Palmer", "Dene", "Java programmer", "male", 33, 2000));
add(new Person("Addison", "Pam", "Java programmer", "female", 34, 1300));
}
};
List phpProgrammers = new ArrayList() {
{
add(new Person("Jarrod", "Pace", "PHP programmer", "male", 34, 1550));
add(new Person("Clarette", "Cicely", "PHP programmer", "female", 23, 1200));
add(new Person("Victor", "Channing", "PHP programmer", "male", 32, 1600));
add(new Person("Tori", "Sheryl", "PHP programmer", "female", 21, 1000));
add(new Person("Osborne", "Shad", "PHP programmer", "male", 32, 1100));
add(new Person("Rosalind", "Layla", "PHP programmer", "female", 25, 1300));
add(new Person("Fraser", "Hewie", "PHP programmer", "male", 36, 1100));
add(new Person("Quinn", "Tamara", "PHP programmer", "female", 21, 1000));
add(new Person("Alvin", "Lance", "PHP programmer", "male", 38, 1600));
add(new Person("Evonne", "Shari", "PHP programmer", "female", 40, 1800));
}
};
//lambda 表达式
//forEach
javaProgrammers.forEach((List p)->System.out.println("FirstName:"p.getFirstName() + ", LastName:" + p.getLastName()));
phpProgrammers.forEach((List p)->System.out.println("FirstName:"p.getFirstName() + ", LastName:" + p.getLastName()));
//过滤器filter
phpProgrammers.stream().filter((List p)->(p->getSalary()>1000))
.forEach((List p)->System.out.println("FirstName:"p.getFirstName() + ", LastName:" + p.getLastName()));
//限制器limit
phpProgrammers.stream().filter((List p)->(p->getSalary()>1000))
.limit(3)
.forEach((List p)->System.out.println("FirstName:"p.getFirstName() + ", LastName:" + p.getLastName()));