package com.sun.first;
/**
* @Author sun
* @Create 2024/6/7 22:51
* @Version 1.0
*/
public class Sample2 {
public static void main(String[] args) {
// 参数和返回值需要与函数对象一致
Fun fun = (a) -> {
return a + 1;
};
System.out.println(fun.test(2));
}
}
// 在编译时检查是否函数式接口有且只有一个抽象方法
@FunctionalInterface
interface Fun {
int test(int a);
}
package com.sun.first;
import java.util.ArrayList;
/**
* @Author sun
* @Create 2024/6/7 22:51
* @Version 1.0
*/
public class Sample2 {
public static void main(String[] args) {
// 参数和返回值需要与函数对象一致
Fun fun = (a) -> {
return a + 1;
};
System.out.println(fun.test(2));
Fun1 fun1 = (int a, int b, int c) -> a + b + c;
Fun2 fun2 = (int a, int b) -> a - b;
Fun3 fun3 = () -> new String();
Fun4 fun4 = () -> new ArrayList<String>();
Fun5<String, Integer> fun5 = (str) -> {
return Integer.valueOf(str);
};
System.out.println(fun5.test("34"));
}
}
// 在编译时检查是否函数式接口有且只有一个抽象方法
@FunctionalInterface
interface Fun {
int test(int a);
}
@FunctionalInterface
interface Fun1 {
int test(int a, int b, int c);
}
@FunctionalInterface
interface Fun2 {
int test(int a, int b);
}
@FunctionalInterface
interface Fun3 {
String test();
}
@FunctionalInterface
interface Fun4 {
ArrayList<String> test();
}
// 参数和返回值都是泛型
@FunctionalInterface
interface Fun5<I, O> {
O test(I input);
}
package com.sun.first;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
* Description: 将判断条件使用函数式接口替换
* @Author sun
* @Create 2024/6/8 21:35
* @Version 1.0
*/
public class Sample4 {
public static void main(String[] args) {
List<Integer> filter = filter(Arrays.asList(1, 2, 3), i -> {
// 这里的参数是Integer类型的,返回值是boolean类型,逻辑是判断i是否是偶数,当然也可以更换
return (i & 1) == 0;
});
System.out.println("filter = " + filter);
}
// 过滤一下list,得到list中的偶数
static List<Integer> filter(List<Integer> list, Predicate<Integer> predicate) {
// 使用一个List来存储结果
List<Integer> res = new ArrayList<>();
// 过滤出偶数
for (Integer i : list) {
// 其中当i的最后一位是1的时候&1才是1,否则为0,也就是偶数
// 这里的函数式接口的意思就是,参数为i,返回值为boolean,也就是如果结果是true,才进行添加
if (predicate.test(i)) {
res.add(i);
}
}
return res;
}
}
package com.sun.first;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
/**
* Description:
* @Author sun
* @Create 2024/6/8 21:57
* @Version 1.0
*/
public class Sample5 {
public static void main(String[] args) {
List<String> map = map(Arrays.asList(1, 2, 3), a -> {
return String.valueOf(a);
} );
System.out.println(map);
}
static List<String> map(List<Integer> list, Function<Integer, String> function) {
List<String> res = new ArrayList<>();
for (Integer i : list) {
// 将结果转换为String类型
// 将具体逻辑使用函数式接口替换,参数为Integer类型的i,返回值为String类型,使用Function
res.add(function.apply(i));
}
return res;
}
}
package com.sun.first;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
/**
* Description:
* @Author sun
* @Create 2024/6/8 22:14
* @Version 1.0
*/
public class Sample6 {
public static void main(String[] args) {
consume(Arrays.asList(1, 3, 3), num -> {
System.out.println("num = " + num);
});
}
static void consume(List<Integer> list, Consumer<Integer> consumer) {
for(Integer num : list) {
// 打印,但是以后逻辑可能改变
// 一个参数,为Integer类型的num,没有返回值,使用Consumer
consumer.accept(num);
}
}
}
package com.sun.first;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;
/**
* Description:
* @Author sun
* @Create 2024/6/8 22:22
* @Version 1.0
*/
public class Sample7 {
public static void main(String[] args) {
List<Integer> supply = supply(3, () -> {
// 逻辑为生成一个随机数
int res = ThreadLocalRandom.current().nextInt();
System.out.println(res);
return res;
});
}
static List<Integer> supply(int count, Supplier<Integer> supplier) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
// 生成随机数,但是逻辑可能改变
// 参数为空,返回值为一个,使用Supplier
result.add(supplier.get());
}
return result;
}
}
package com.sun.methodref;
import java.util.stream.Stream;
/**
* Description:
* @Author sun
* @Create 2024/6/10 21:19
* @Version 1.0
*/
public class MethodRef01 {
public static void main(String[] args) {
Stream.of(
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).forEach(student -> {
// 挑选出所有的男的
System.out.println(student);
});
}
}
class Student {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student(String name, String sex) {
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class MethodRef01 {
public static void main(String[] args) {
Stream.of(
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).forEach(MethodRef01::abc);
// 对于forEach来说,参数为每个元素,返回值为空
// 所以无论是lambda表达式还是方法引用,他们都遵循这个条件:(Student stu) -> void
}
public static void abc(Student student) {
System.out.println(student);
}
}
public static void main(String[] args) {
Stream.of(
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).filter(student -> {
// (Student student) -> boolean
if (student.getSex().equals("男")) {
return true;
}
return false;
})
.forEach(MethodRef01::abc);
}
public static void abc(Student student) {
System.out.println(student);
}
}
public static void main(String[] args) {
Stream.of(
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).filter(MethodRef01::filter).
forEach(MethodRef01::abc);
}
// (Student student) -> boolean
public static boolean filter(Student student) {
return student.getSex().equals("男");
}
public static void abc(Student student) {
System.out.println(student);
}
public static void main(String[] args) {
Stream.of(
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).filter(student -> {
// 使用lambda表达式
return student.isMale(student);
})
.forEach(MethodRef01::abc);
}
public void main(String[] args) {
Stream.of(
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).filter(Student::isMale)
.forEach(MethodRef01::abc);
}
package com.sun.first;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Description:
* @Author sun
* @Create 2024/6/16 21:42
* @Version 1.0
*/
public class Sample9 {
public static void main(String[] args) {
// 调用无参构造
Supplier<Student> m1 = Student::new;
Student student = m1.get();
System.out.println("student = " + student);
// 调用一个参数的构造
Function<String, Student> m2 = Student::new;
Student sun = m2.apply("sun");
System.out.println("sun = " + sun);
// 调用两个参数的构造
BiFunction<String, String, Student> m3 = Student::new;
Student apply = m3.apply("sun", "男");
System.out.println("apply = " + apply);
}
}
package com.sun.first;
import java.util.stream.Stream;
/**
* Description:
* @Author sun
* @Create 2024/6/17 21:18
* @Version 1.0
*/
public class Sample10 {
/**
* 判断学生是否是男的
* @param student
* @return
*/
Boolean isMan(Student student) {
return student.getSex().equals("男");
}
public void main(String[] args) {
Stream.of(
// 三个Student对象
new Student("libai", "男"),
new Student("dufu", "男"),
new Student("lishimin", "女")
).filter(this::isMan);
}
}