一、lambda表达式
能够使用lambda表达式改写的前提:【同时满足】
1、必须要有一个接口
2、接口中有且仅只能有一个抽象方法的时候
代码案例
public class LambdaDemo1 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("hadoop");
list.add("java");
//lambda表达式的写法
list.forEach(e-> System.out.println(e+"-"+e.length()));
list.forEach(e-> System.out.println(e+": 李刚"));
// list.forEach();
}
}
二、案例
员工类
public class Staff {
private String id;
private String name;
private int age;
private int salary;
public Staff() {
}
public Staff(String id, String name, int age, int salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Staff{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
1、方法实现
public class StaffDemo {
private static final List<Staff> list = Arrays.asList(
new Staff("sj1001","李刚",18,35000),
new Staff("sj1002","钱志强",13,20000),
new Staff("sj1003","江川",24,50000),
new Staff("sj1004","祝帅",16,21000),
new Staff("sj1005","吴问强",8,8000)
);
public static void main(String[] args) {
// 需求1:过滤出薪资大于30000的员工
List<Staff> staffs1 = filterWithSalary(list);
for (Staff staff : staffs1) {
System.out.println(staff);
}
System.out.println("--------------------------------");
// 需求2:过滤出年龄小于18的员工
List<Staff> staffs2= filterWithAge(list);
for (Staff staff : staffs2) {
System.out.println(staff);
}
}
public static List<Staff> filterWithAge(List<Staff> list){
ArrayList<Staff> list2 = new ArrayList<>();
for (Staff staff : list) {
if(staff.getAge()<18){
list2.add(staff);
}
}
return list2;
}
public static List<Staff> filterWithSalary(List<Staff> list){
ArrayList<Staff> list2 = new ArrayList<>();
for (Staff staff : list) {
if(staff.getSalary()>30000){
list2.add(staff);
}
}
return list2;
}
}
2、使用接口改进
public interface FilterStaff {
boolean filterWithCondition(Staff staff);
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class FilterStaffImpl implements FilterStaff{
@Override
public boolean filterWithCondition(Staff staff) {
return staff.getSalary()>30000;
}
}
class FilterStaffImpl2 implements FilterStaff{
@Override
public boolean filterWithCondition(Staff staff) {
return staff.getAge()<18;
}
}
public class StaffDemo {
private static final List<Staff> list = Arrays.asList(
new Staff("sj1001","李刚",18,35000),
new Staff("sj1002","钱志强",13,20000),
new Staff("sj1003","江川",24,50000),
new Staff("sj1004","祝帅",16,21000),
new Staff("sj1005","吴问强",8,8000)
);
public static void main(String[] args) {
// 需求1:过滤出薪资大于30000的员工
// List<Staff> staffs1 = filterWithSalary(list);
List<Staff> staffs1 = filterWithCondition(list, new FilterStaffImpl());
for (Staff staff : staffs1) {
System.out.println(staff);
}
System.out.println("--------------------------------");
// 需求2:过滤出年龄小于18的员工
// List<Staff> staffs2= filterWithAge(list);
List<Staff> staffs2 = filterWithCondition(list, new FilterStaffImpl2());
for (Staff staff : staffs2) {
System.out.println(staff);
}
}
public static List<Staff> filterWithCondition(List<Staff> list, FilterStaff filterStaff){
ArrayList<Staff> list2 = new ArrayList<>();
for (Staff staff : list) {
if(filterStaff.filterWithCondition(staff)){
list2.add(staff);
}
}
return list2;
}
// public static List<Staff> filterWithAge(List<Staff> list){
// ArrayList<Staff> list2 = new ArrayList<>();
// for (Staff staff : list) {
// if(staff.getAge()<18){
// list2.add(staff);
// }
// }
// return list2;
// }
//
// public static List<Staff> filterWithSalary(List<Staff> list){
// ArrayList<Staff> list2 = new ArrayList<>();
// for (Staff staff : list) {
// if(staff.getSalary()>30000){
// list2.add(staff);
// }
// }
// return list2;
// }
}
3、使用匿名内部类改进
public class StaffDemo {
private static final List<Staff> list = Arrays.asList(
new Staff("sj1001","李刚",18,35000),
new Staff("sj1002","钱志强",13,20000),
new Staff("sj1003","江川",24,50000),
new Staff("sj1004","祝帅",16,21000),
new Staff("sj1005","吴问强",8,8000)
);
public static void main(String[] args) {
// 需求1:过滤出薪资大于30000的员工
// List<Staff> staffs1 = filterWithSalary(list);
List<Staff> staffs1 = filterWithCondition(list, new FilterStaff() {
@Override
public boolean filterWithCondition(Staff staff) {
return staff.getSalary()>30000;
}
});
for (Staff staff : staffs1) {
System.out.println(staff);
}
System.out.println("--------------------------------");
// 需求2:过滤出年龄小于18的员工
// List<Staff> staffs2= filterWithAge(list);
List<Staff> staffs2 = filterWithCondition(list, new FilterStaff() {
@Override
public boolean filterWithCondition(Staff staff) {
return staff.getAge()<18;
}
});
for (Staff staff : staffs2) {
System.out.println(staff);
}
}
public static List<Staff> filterWithCondition(List<Staff> list, FilterStaff filterStaff){
ArrayList<Staff> list2 = new ArrayList<>();
for (Staff staff : list) {
if(filterStaff.filterWithCondition(staff)){
list2.add(staff);
}
}
return list2;
}
// public static List<Staff> filterWithAge(List<Staff> list){
// ArrayList<Staff> list2 = new ArrayList<>();
// for (Staff staff : list) {
// if(staff.getAge()<18){
// list2.add(staff);
// }
// }
// return list2;
// }
//
// public static List<Staff> filterWithSalary(List<Staff> list){
// ArrayList<Staff> list2 = new ArrayList<>();
// for (Staff staff : list) {
// if(staff.getSalary()>30000){
// list2.add(staff);
// }
// }
// return list2;
// }
}
4、使用lambda表达式改进
public class StaffDemo {
private static final List<Staff> list = Arrays.asList(
new Staff("sj1001", "李刚", 18, 35000),
new Staff("sj1002", "钱志强", 13, 20000),
new Staff("sj1003", "江川", 24, 50000),
new Staff("sj1004", "祝帅", 16, 21000),
new Staff("sj1005", "吴问强", 8, 8000)
);
public static void main(String[] args) {
// 需求1:过滤出薪资大于30000的员工
// List<Staff> staffs1 = filterWithSalary(list);
// List<Staff> staffs1 = filterWithCondition(list, new FilterStaff() {
// @Override
// public boolean filterWithCondition(Staff staff) {
// return staff.getSalary() > 30000;
// }
// });
List<Staff> staffs1 = filterWithCondition(list, (Staff s)->{
return s.getSalary()>30000;
});
for (Staff staff : staffs1) {
System.out.println(staff);
}
System.out.println("--------------------------------");
// 需求2:过滤出年龄小于18的员工
// List<Staff> staffs2= filterWithAge(list);
// List<Staff> staffs2 = filterWithCondition(list, new FilterStaff() {
// @Override
// public boolean filterWithCondition(Staff staff) {
// return staff.getAge()<18;
// }
// });
//使用lambda表达式改写
List<Staff> staffs2 = filterWithCondition(list, (Staff s) -> {
return s.getAge()<18;
});
// List<Staff> staffs2 = filterWithCondition(list, s->s.getAge()<18);
for (Staff staff : staffs2) {
System.out.println(staff);
}
}
public static List<Staff> filterWithCondition(List<Staff> list, FilterStaff filterStaff) {
ArrayList<Staff> list2 = new ArrayList<>();
for (Staff staff : list) {
if (filterStaff.filterWithCondition(staff)) {
list2.add(staff);
}
}
return list2;
}
// public static List<Staff> filterWithAge(List<Staff> list){
// ArrayList<Staff> list2 = new ArrayList<>();
// for (Staff staff : list) {
// if(staff.getAge()<18){
// list2.add(staff);
// }
// }
// return list2;
// }
//
// public static List<Staff> filterWithSalary(List<Staff> list){
// ArrayList<Staff> list2 = new ArrayList<>();
// for (Staff staff : list) {
// if(staff.getSalary()>30000){
// list2.add(staff);
// }
// }
// return list2;
// }
}
三、lambda表达式的缩写
public class LambdaDemo {
public static void main(String[] args) {
/**
* 场景1
*/
//无参数,无返回值
// show1(()->{
// System.out.println("好好学习,天天向上!");
// });
// 如果方法逻辑体只有一行的时候,可以将大括号省略
// show1(()-> System.out.println("好好学习,天天向上!"));
/**
* 场景2
*/
// 有一个参数,无返回值
// show2((String s)->{
// System.out.println(s+": 江川");
// });
// 左边的参数类型可以省略不写, jvm会做自动类型上下文推断
// show2((s) -> System.out.println(s + ": 江川"));
// 若只有一个参数,小括号可以省略不写
// show2(s -> System.out.println(s + ": 江川"));
/**
* 场景3
*/
//有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
// show3((String s1,String s2)->{
// return s1+"-数加-"+s2;
// });
// 若Lambda 体中只有一条语句,return和大括号都可以省略不写
// show3((s1, s2) -> s1 + "-数加-" + s2);
}
public static void show3(Inter3 inter3) {
System.out.println(inter3.fun1("hello", "world"));
}
public static void show2(Inter2 inter2) {
inter2.fun1("hello");
}
public static void show1(Inter1 inter1) {
inter1.fun1();
}
}
四、java内置函数式接口
1、断言式接口
java内置函数式接口:断言式接口
只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
public class StaffDemo {
private static final List<Staff> list = Arrays.asList(
new Staff("sj1001", "李刚", 18, 35000),
new Staff("sj1002", "钱志强", 13, 20000),
new Staff("sj1003", "江川", 24, 50000),
new Staff("sj1004", "祝帅", 16, 21000),
new Staff("sj1005", "吴问强", 8, 8000)
);
public static void main(String[] args) {
// 需求1:过滤出薪资大于30000的员工
List<Staff> staffs1 = filterWithCondition(list, s -> s.getSalary() > 30000);
for (Staff staff : staffs1) {
System.out.println(staff);
}
System.out.println("--------------------------------");
// 需求2:过滤出年龄小于18的员工
//使用lambda表达式改写
List<Staff> staffs2 = filterWithCondition(list, s -> s.getAge() < 18);
for (Staff staff : staffs2) {
System.out.println(staff);
}
}
public static List<Staff> filterWithCondition(List<Staff> list, Predicate<Staff> predicate) {
ArrayList<Staff> list2 = new ArrayList<>();
for (Staff staff : list) {
if (predicate.test(staff)) {
list2.add(staff);
}
}
return list2;
}
}
2、函数型接口
java内置函数式接口:函数型接口
只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
public class Demo {
public static void main(String[] args) {
//随机生成10个1-100的数据到集合返回
List<Integer> list = getList(() -> new Random().nextInt(100) + 1);
for (Integer i : list) {
System.out.println(i);
}
}
public static List<Integer> getList(Supplier<Integer> supplier){
ArrayList<Integer> list = new ArrayList<>();
for(int i=1;i<11;i++){
list.add(supplier.get());
}
return list;
}
}
3、供给型接口
java内置函数式接口:供给型接口
只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
@FunctionalInterface
public interface Supplier<T> {
T get();
}
public class Demo {
public static void main(String[] args) {
//随机生成10个1-100的数据到集合返回
List<Integer> list = getList(() -> new Random().nextInt(100) + 1);
for (Integer i : list) {
System.out.println(i);
}
}
public static List<Integer> getList(Supplier<Integer> supplier){
ArrayList<Integer> list = new ArrayList<>();
for(int i=1;i<11;i++){
list.add(supplier.get());
}
return list;
}
}
4、消费型接口
java内置函数式接口:消费型接口
只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
public class Demo {
public static void main(String[] args) {
Staff s = new Staff("sj1001", "李刚", 18, 35000);
fun1(s, liGang -> System.out.println("员工编号:" + liGang.getId() + ", 姓名:" + liGang.getName() + ", 年龄:" + liGang.getAge() + ", 薪资:" + liGang.getSalary()));
}
public static void fun1(Staff staff, Consumer<Staff> consumer) {
consumer.accept(staff);
}
}
五、lambda表达式简写情况
* 假如 Lambda 表达式符合如下格式:
* ([变量1, 变量2, ...]) -> 类名.静态方法名([变量1, 变量2, ...])
* 我们可以简写成如下格式:
* 类名::静态方法名
代码案例:
public class Demo1 {
public static void main(String[] args) {
Staff s1 = new Staff("sj1001", "李刚", 18, 35000);
Staff s2 = new Staff("sj1002", "钱志强", 13, 20000);
Staff s3 = new Staff("sj1003", "江川", 24, 50000);
Staff s4 = new Staff("sj1004", "祝帅", 16, 21000);
Staff s5 = new Staff("sj1005", "吴问强", 8, 8000);
// List<Staff> staffList = fun1(staffs -> Arrays.asList(staffs), s1, s2, s3, s4, s5);
/**
* 假如 Lambda 表达式符合如下格式:
* ([变量1, 变量2, ...]) -> 类名.静态方法名([变量1, 变量2, ...])
* 我们可以简写成如下格式:
* 类名::静态方法名
*/
List<Staff> staffList = fun1(Arrays::asList, s1, s2, s3, s4, s5);
for (Staff staff : staffList) {
System.out.println(staff);
}
}
public static List<Staff> fun1(Inter inter, Staff... staffs){
return inter.getStaff(staffs);
}
}
* 假如 Lambda 表达式符合如下格式:
* ([变量1, 变量2, ...]) -> 对象引用.方法名([变量1, 变量2, ...])
* 我们可以简写成如下格式:
* 对象引用::方法名
代码案例:
class Demo2 {
public String show(String s1, String s2) {
return s1 + "-数加-" + s2;
}
}
interface Inter{
String fun1(String a,String b);
}
public class Demo {
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
// show2("hello","ligang",(s1,s2)->demo2.show(s1,s2));
show2("hello","ligang",demo2::show);
}
public static void show2(String s1,String s2,Inter inter){
String s = inter.fun1(s1, s2);
System.out.println(s);
}
}
* 假如我们的 Lambda 表达式符合如下格式:
* (变量1[, 变量2, ...]) -> 变量1.实例方法([变量2, ...])
* 那么我们的代码就可以简写成:
* 变量1对应的类名::实例方法
代码案例:
interface Inter {
String fun1(String s1, int i, int j);
}
public class Demo {
public static void main(String[] args) {
// show1((s1, i, j) -> s1.substring(i, j));
show1(String::substring);
}
public static void show1(Inter inter) {
String s = inter.fun1("李刚是世界上最有男人味的男人", 8, 11);
System.out.println(s);
}
}
* 假如我们的 Lambda 表达式符合如下格式:
* ([变量1, 变量2, ...]) -> new 类名([变量1, 变量2, ...])
* 我们就可以简写成如下格式:
* 类名::new
代码案例:
public class Demo {
public static void main(String[] args) {
// show("sj1001","吴问强",18,100000, (s1,s2,i1,i2)->new Staff(s1,s2,i1,i2));
/**
* 假如我们的 Lambda 表达式符合如下格式:
* ([变量1, 变量2, ...]) -> new 类名([变量1, 变量2, ...])
* 我们就可以简写成如下格式:
* 类名::new
*/
show("sj1001","吴问强",18,100000, Staff::new);
}
public static void show(String s1,String s2,int i1,int i2,Inter inter){
Staff staff = inter.fun1(s1, s2, i1, i2);
System.out.println(staff);
}
}
* 假如我们的 Lambda 表达式符合如下格式:
* (变量) -> new 元素类型[变量]
* 我们就可以简写成如下格式:
* 元素类型[] :: new
public class Demo {
public static void main(String[] args) {
// show(7, i->new int[i]);
/**
* 假如我们的 Lambda 表达式符合如下格式:
* (变量) -> new 元素类型[变量]
* 我们就可以简写成如下格式:
* 元素类型[] :: new
*/
show(7, int[]::new);
}
public static void show(int i,Inter inter){
int[] ints = inter.fun(i);
System.out.println(ints.length);
}
}
六、枚举
1. 创建枚举类的属性(成员遍历),必须是作为私有常量出现
2. 必须将构造方法私有化,这是为了保证类的对象是有限个的目的
3. 提供公共的静态的final方法给外界获取枚举类中多个对象
4. 提供公共的获取属性的方法
5. 重写toString()方法
代码案例:
class Season{
//1. 创建枚举类的属性(成员遍历),必须是作为私有常量出现
private String name;
private String info;
//2. 必须将构造方法私有化,这是为了保证类的对象是有限个的目的
private Season(String name, String info){
this.name = name;
this.info = info;
}
//3. 提供公共的静态的final方法给外界获取枚举类中多个对象
public static final Season SPRING = new Season("春天","春暖花开");
public static final Season SUMMER = new Season("夏天","烈日炎炎");
public static final Season AUTUMN = new Season("秋天","秋高气爽");
public static final Season WINTER = new Season("冬天","白雪皑皑");
//4. 提供公共的获取属性的方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
}
public class SeasonDemo1 {
public static void main(String[] args) {
Season spring = Season.SPRING;
Season summer = Season.SUMMER;
}
}
jdk1.5之后 java提供了一个关键字用于创建枚举类 enum
代码案例:
enum Season2{
//jdk1.5之后必须将有限个对象放在enum枚举类第一个开头位置,多个对象之间使用逗号隔开
SPRING("春天","春暖花开"),
SUMMER("夏天","烈日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","白雪皑皑");
//1. 创建枚举类的属性(成员遍历),必须是作为私有常量出现
private String name;
private String info;
//2. 必须将构造方法私有化,这是为了保证类的对象是有限个的目的
private Season2(String name, String info){
this.name = name;
this.info = info;
}
//4. 提供公共的获取属性的方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
}
public class SeasonDemo2 {
public static void main(String[] args) {
Season2 spring = Season2.SPRING;
System.out.println(spring.getName());
System.out.println(spring.getInfo());
}
}
标签:java,String,staff,list,day20,new,lambda,public,Staff
From: https://www.cnblogs.com/w-ll/p/18476047