Java教程

Functional Programming in Java

本文主要是介绍Functional Programming in Java,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • Functional Programming in Java
    • Lambda表达式
    • forEach
    • Imperative 和 declarative

Functional Programming in Java

参考资料:Functional Programming in Java with Examples

Lambda表达式

语法

(参数) -> 函数体

一个测试例子:把run方法封装在Runnable中。

匿名类

Java 中可以实现一个类中包含另外一个类,且不需要提供任何的类名直接实例化。

主要是用于在我们需要的时候创建一个对象来执行特定的任务,可以使代码更加简洁。

匿名类是不能有名字的类,它们不能被引用,只能在创建时用 new 语句来声明它们。

匿名类通常继承一个父类或实现一个接口。

匿名类的语法如下:

在这里插入图片描述

参考:菜鸟教程

这里Runnable接口是线程辅助类,仅定义了一个方法run()方法,用来实现多线程。

import java.util.Arrays;
import java.util.List;
public class Test {
    public static void main(String[] args)
    {
        // Defination of an anonymous method
        Runnable r = new Runnable() {
            public void run() {
                System.out.println(
                    "Running in Runnable thread");
            }
        };

        r.run();
        System.out.println(
            "Running in main thread");
    }

}

执行结果

Running in Runnable thread
Running in main thread

lambda表达式的写法

import java.util.Arrays;
import java.util.List;
public class Test2 {
	public static void main(String[] args)
	{
		// lambda expression
        Runnable r
			= ()
			-> System.out.println(
				"Running in Runnable thread"
			);

		r.run();
		System.out.println(
			"Running in main thread");	
	}

}

Now, the above code has been converted into Lambda expressions rather than the anonymous method. Here we have evaluated a function that doesn’t have any name and that function is a lambda expression. So, in this case, we can see that a function has been evaluated and assigned to a runnable interface and here this function has been treated as the first-class citizen.

我们将Java7中的匿名方法转化为Lambda表达式

forEach

// Java 8 program to demonstrate
// an internal iterator

import java.util.Arrays;
import java.util.List;

public class Test1 {
	public static void main(String[] args)
	{
		List<Integer> numbers
			= Arrays.asList(11, 22, 33, 44,
							55, 66, 77, 88,
							99, 100);

		// External iterator
		for (int i = 0; i < numbers.size(); i ++) {
			System.out.print(numbers.get(i) + " ");
		}
		System.out.println();
		// Internal iterator
		numbers.forEach(number
						-> System.out.print(
							number + " "));
		System.out.println();
		numbers.forEach(System.out::println);
	}
}

Imperative 和 declarative

// Java program to find the sum
// using imperative style of coding
import java.util.Arrays;
import java.util.List;
public class Test1 {
	public static void main(String[] args)
	{
		List<Integer> numbers
			= Arrays.asList(11, 22, 33, 44,
							55, 66, 77, 88,
							99, 100);

		int result = 0;

		// difference between Imperative and Declarative

		// 必须不停改变变量result
		for (Integer number : numbers) {
			if (number % 2 == 0)
				result += number;
		}
		System.out.println(result); // output: 320
		
		// 不用不停更改变量的值,而是把数据传到不同的函数
		// 都是pure function,没有负作用
		// should never try mutating any variable which is used inside pure functions.
		System.out.println(
			numbers.stream()
				.filter(number -> number % 2 == 0)
				.mapToInt(number -> number * 2)  
				.sum()
		);
		// output: 640
	}
}

后记:
在课上学习软件设计,老师在讲higher-order function,涉及到Haskell 和 Java,自己去复习一下相关知识。这里暂且记录一下java的函数式编程,作为自己的记录。后续有丰富相关文字的可能。

这篇关于Functional Programming in Java的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!