ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Functional Programming in Java

2022-01-30 19:59:16  阅读:183  来源: 互联网

标签:Java java Programming Functional System println number public out


文章目录

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的函数式编程,作为自己的记录。后续有丰富相关文字的可能。

标签:Java,java,Programming,Functional,System,println,number,public,out
来源: https://blog.csdn.net/shizheng_Li/article/details/122755334

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有