Java函数式编程常用案例学习
Java函数式编程常用案例学习
一、Optional API
1. 创建Optional实例
1.1 空Optional
1 | Optional<String> emptyOpt = Optional.empty(); |
1.2 非null值的Optional
1 | String str = "Hello"; |
1.3 可能为null的Optional
1 | String str = null; |
2. 判断值是否存在
2.1 判断值存在
1 | Optional<String> opt = Optional.of("Hello"); |
2.2 判断值不存在(Java 11+)
1 | Optional<String> opt = Optional.empty(); |
3. 获取值
3.1 直接获取值
1 | Optional<String> opt = Optional.of("Hello"); |
3.2 默认值获取(orElse)
1 | Optional<String> opt = Optional.empty(); |
3.3 延迟加载默认值(orElseGet)
1 | Optional<String> opt = Optional.empty(); |
3.4 值不存在时抛出异常(orElseThrow)
1 | Optional<String> opt = Optional.empty(); |
4. 条件操作
4.1 值存在时执行操作
1 | Optional<String> opt = Optional.of("Hello"); |
4.2 值存在或不存在时执行不同操作(Java 9+)
1 | Optional<String> opt = Optional.empty(); |
5. 过滤和映射
5.1 过滤值
1 | Optional<String> opt = Optional.of("Hello World"); |
5.2 映射值
1 | Optional<String> opt = Optional.of("hello"); |
5.3 扁平映射
1 | Optional<String> opt = Optional.of("hello"); |
二、Stream API
1. 创建流
1.1 从集合创建流
1 | List<String> list = Arrays.asList("a", "b", "c"); |
1.2 从数组创建流
1 | String[] array = {"a", "b", "c"}; |
1.3 创建指定元素的流
1 | Stream<String> stream = Stream.of("a", "b", "c"); |
2. 中间操作
2.1 过滤(filter)
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); |
2.2 映射(map)
1 | List<String> words = Arrays.asList("hello", "world"); |
2.3 排序(sorted)
1 | List<Integer> numbers = Arrays.asList(3, 1, 2); |
2.4 去重(distinct)
1 | List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3); |
2.5 限制数量(limit)和跳过元素(skip)
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); |
3. 终端操作
3.1 遍历(forEach)
1 | List<String> list = Arrays.asList("a", "b", "c"); |
3.2 收集结果(collect)
1 | List<String> list = Arrays.asList("a", "b", "c"); |
3.3 计数(count)
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); |
3.4 匹配(anyMatch, allMatch, noneMatch)
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); |
3.5 查找(findFirst, findAny)
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); |
4. 收集器应用
4.1 分组(groupingBy)
1 | List<String> words = Arrays.asList("apple", "banana", "cat", "dog"); |
4.2 字符串拼接(joining)
1 | List<String> words = Arrays.asList("Hello", "World"); |
4.3 汇总统计(summarizingInt)
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); |
三、函数式编程
1. Lambda表达式基础
1.1 无参数无返回值
1 | Runnable runnable = () -> System.out.println("Hello Lambda"); |
1.2 单参数
1 | Consumer<String> printer = s -> System.out.println(s); |
1.3 多参数和返回值
1 | BinaryOperator<Integer> adder = (a, b) -> a + b; |
2. 函数式接口
2.1 Predicate(断言)
1 | Predicate<Integer> isPositive = n -> n > 0; |
2.2 Function(函数)
1 | Function<String, Integer> strLength = s -> s.length(); |
2.3 Consumer(消费者)
1 | Consumer<String> logger = s -> System.out.println("Log: " + s); |
2.4 Supplier(供应商)
1 | Supplier<Double> randomValue = () -> Math.random(); |
3. 方法引用
3.1 静态方法引用
1 | Function<String, Integer> parseInt = Integer::parseInt; |
3.2 实例方法引用
1 | String str = "Hello"; |
3.3 类的任意对象的方法引用
1 | Function<String, String> toUpperCase = String::toUpperCase; |
3.4 构造函数引用
1 | Supplier<List<String>> listSupplier = ArrayList::new; |
4. 函数组合
4.1 函数链(andThen)
1 | Function<Integer, Integer> add5 = x -> x + 5; |
4.2 函数组合(compose)
1 | Function<Integer, Integer> add5 = x -> x + 5; |
4.3 谓词组合(and, or, negate)
1 | Predicate<Integer> isEven = n -> n % 2 == 0; |
5. 变量作用域
5.1 访问外部变量(effectively final)
1 | int num = 10; // 隐式final |
5.2 线程安全的Lambda
1 | List<Integer> list = new ArrayList<>(); |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Nosaw博客!
评论




