lambda 表达式可以用非常少的代码实现抽象方法。lambda 表达式不能独立执行,因此必须实现函数式接口,并且会返回一个函数式接口的对象。lambda表达式的语法非常特殊语法如下:
() -> 结果表达式
//无参
参数 -> {代码块}
//一个参数
(参数1,参数2,...,参数n) -> {代码块}
//多个参数
例题:
package 十四;
interface SayHi{
String say();
}
public class NoParamDemo {
public static void main(String[] args) {
SayHi sh1 = new SayHi() {
public String say() {
return "这是匿名内部类";
}
};
System.out.print(sh1.say());
SayHi sh2 = () ->{return "这是匿名内部类";};
System.out.print(sh2.say());
}
}
如果抽象方法中只有一个参数,lambda表达式则可以省略圆括号
package 十四;
interface AddInt{
int add(int a,int b);
}
public class ParamDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
AddInt ai1 = new AddInt() {
public int add(int a,int b) {
return a + b;
}
};
System.out.println("匿名内部类:" + ai1.add(3,5));
AddInt ai2 = (a,b) -> {return a + b;};
System.out.println("匿名内部类:" + ai2.add(3,5));
}
}
lambda表达式会自动判断返回值类型是否符合抽象方法的定义
package 十四;
interface CheckGrade{
String check(int grade);
}
public class GradeDemo {
public static void main(String[] args) {
CheckGrade g = (n) ->{
if (n >= 90 && n <= 100){
return "成绩为优";
} else if (n >= 80 && n < 90) {
return "成绩为良";
}else if (n >= 60 && n < 80) {
return "成绩为中";
}else if (n >= 00 && n < 60) {
return "成绩无差";
}else {
return "成绩无效";
}
};
System.out.println(g.check(89));
}
}
语法:
类名::静态方法名
新的操作符“::”,中间无空格,左边表示方法所属的类名,右边是方法名,语法中方法名是没有括号的
例题
package 十四;
interface StaticMethodInteeface{
int method(int a,int b);
}
public class StaticMethodDemo {
static int add(int a,int b) {
return a + b;
}
public static void main(String[] args) {
StaticMethodInteeface sm = StaticMethodDemo::add;
int result = sm.method(15, 16);
System.out.println("接口方法结果:" + result);
}
}
引用成员方法的语法:
对象名::成员方法名
操作符左侧必须是一个对象名,而不是类名
package 十四;
import java.text.SimpleDateFormat;
import java.util.Date;
interface InstanceMethodInterface{
String method(Date date);
}
public class InstanceMethodDemo {
public String format(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
public static void main(String[] args) {
InstanceMethodDemo demo = new InstanceMethodDemo();
InstanceMethodInterface im = demo::format;
Date date = new Date();
System.out.println("默认格式" + date);
System.out.println("接口输出模式" + im.method(date));
}
}
package 十四;
interface ConstructorsInterface{
ConstructorsDemo action(int a);
}
public class ConstructorsDemo {
public ConstructorsDemo() {
System.out.print("无参构造方法");
}
public ConstructorsDemo(int a) {
System.out.print("有参构造方法" + a);
}
public static void main(String[] args) {
ConstructorsInterface ci = ConstructorsDemo::new;
ci.action(8);
}
}
流处理有点类似数据库的 SQL 语句,可以执行非常复杂的过滤、映射、查找和收集功能,并且代码量很少。唯一的缺点是代码可读性不高。
对员工数据进行流处理,常见员工表类
package 十四;
import java.util.ArrayList;
import java.util.List;
public class Employee {
private String name;
private int age;
private double salary;
private String sex;
private String dept;
public Employee(String name, int age, double salary, String sex, String depy) {
super();
this.name = name;
this.age = age;
this.salary = salary;
this.sex = sex;
this.dept = depy;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + ", sex=" + sex + ", depy=" + dept
+ "]";
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getSex() {
return sex;
}
public String getDept() {
return dept;
}
static List getEmployee(){
List list = new ArrayList();
list.add(new Employee("老张",40,9000.0,"男","运营部"));
list.add(new Employee("小刘",24,5000.0,"女","开发部"));
list.add(new Employee("大纲",32,7500.0,"男","销售部"));
list.add(new Employee("翠花",28,5500.0,"女","销售部"));
list.add(new Employee("小马",21,3000.0,"男","开发部"));
list.add(new Employee("老王",35,6000.0,"女","人事部"));
list.add(new Employee("小王",21,3000.0,"女","人事部"));
return list;
}
}
用流筛选年龄大与30
package 十四;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamDemo {
public static void main(String[] args) {
List list = Employee.getEmployee();
Stream stream = list.stream();
stream = stream.filter(people -> people.getAge() > 30) //筛选年龄大于30的员工
.limit(1);//限制条数
List result = stream.collect(Collectors.toList());
for(Employee emp : result) {
System.out.println(emp);
}
}
}
用流筛选
package 十四;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GroupDemo {
public static void main(String[] args) {
List list = Employee.getEmployee();
Stream stream = list.stream();
Map> map = stream.collect(Collectors.groupingBy(Employee::getDept));
Set depts = map.keySet();
for(String dept : depts) {
System.out.println(dept + "员工列表如下");
List temp = map.get(dept);
for(Employee e : temp) {
System.out.println(e);
}
System.out.println();
}
}
}