简要介绍JAVA8中的函数式编程;Brief Introduction to Functional Programming in Java 8
来源:互联网 发布:sql 2005什么用 编辑:程序博客网 时间:2024/05/01 00:59
Brief Introduction to Functional Programming in Java 8
Introduction
Recently, I had the chance to do some work with Java 8. Since until then, I had experience with Java until version 7, I had to learn some new concepts. In this tip, I would like to describe what I have learned in Java 8 and share it with you.
In Java 8, some elements of functional programming were added to the language. The main purpose for that is to better support multicore programming.
Multicore programming is a very current topic. Nowadays, the increase of power computation is mainly achieved by increasing the number of CPUs on a single board. The increase of CPU clock frequency was common in the past and continued until the early 2000s, when it stopped because of technological limitations.
Functional programming is a programming paradigm that is particularly suited for multicore programming. Functional programming is based on evaluating functions. Examples of functions are x==2 or x+3, where x is an input parameter. When x is 2 then x+3 evaluates 5.
It is very important to point out that these functions do not modify the input parameters nor variables. In other words, these functions do not change the variables of a program. As a result, these functions can be easily parallelized since there is no risk that function1
and function2
will modify variable1
at the same time. This is a core concept of functional programming.
Java 8 is an object oriented language with support for functional programming. Functional programming in Java 8 involves working with objects, methods but also with functions.
The functions used in Java 8 are special kind of functions called lambda expressions.
Lambda Expressions
A Lambda expression is essentially a function without name and is represented as follows:
(params) -> function body
A Lambda expression consists of three parts:
- List of parameters (left of arrow)
- Arrow
- Function body
Some examples of Lambda expressions are:
(1) (2) (3)
x -> x + 2
(1) (2) (3)
(y,z) -> y * z * 2
Lambda expressions can be used in general when you want to pass a piece of code as parameter.
Although the main purpose of functional programming in Java 8 is to simplify the multicore programming, any type of program can benefit from using lambda expressions.
In Java 8, there are two main uses for lambda expressions:
- Use lambda expressions instead of anonymous classes
- Write operations on collections that can be easily parallelized
Use of Lambda Expressions Instead of Anonymous Classes
You can replace any anonymous class with a lambda expression when the anonymous class has just one method. Anonymous classes are also an attempt to pass a piece of code as parameter.
For example, you can pass a piece of code to be executed to a button when it is clicked in two ways, with anonymous classes and with lambda expressions:
// button click with anonymous classesbutton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); }});// button click with lambda expressionbutton.addActionListener(event -> System.out.println("button clicked"));
The code with Lambda expression is much more compact.
Use Lambda Expressions to Write Operations on Collections That Can Be Easily Parallelized
Lambda expression can be used on collections by the Stream
API.
The Stream
API is a new interface introduced in Java 8 that makes it possible to write functional code on collections with lambda expressions. You can think of the Stream
as a disguised iterator that allows you to examine all the elements of the collection.
Let's see an example: given a collection of Integers, you want to find the number of values larger than 5
.
Without Lambda expressions and Stream
API, you would write a piece of code such as:
// code before Java 8int counter = 0;for (int val : myIntList) { if(val > 5) counter++;}
The code with lambda expressions looks like:
// functional approach with stream and Lambda expressionsint counter = myIntList.stream().filter(n -> n>5).count();
With the stream
API, you can call the filter
method and pass that lambda expression. You can read the code as follows "give me all values from the collection that satisfy the condition n>5
and count them".
The code with lambda expression has a functional flavor because you pass the Lambda expression n-> n>5
as parameter to the filter
function.
Notice that with the lambda expressions, you focus more on what result you want (all values > 5) and not how you get it (this is a matter of the compiler).
When a stream
function returns another stream
, you can create chains of stream
oerations. In the examplestream()
and filter()
return a stream
, so they can be followed by another operation. By contrast, count()
returns an int
so it cannot be followed by another stream
operation.
If you want to parallelize the code on the collection, you can just replace stream
with parallelStream
.
<code>int counter1 = myIntList.parallelStream().filter(n -> n>5).count();</code>
When to use stream
or parallelStream
is not a trivial task and beyond the purposes of this tip. Among other things, this will depend on the size of the collection, the number of the available CPUs. In the end, you will need to make comparative tests.
http://www.codeproject.com/Tips/1069832/Brief-Introduction-to-Functional-Programming-in-Ja
- 简要介绍JAVA8中的函数式编程;Brief Introduction to Functional Programming in Java 8
- Introduction to Functional Programming in Swift
- Introduction to C++ Programming in UE4 介绍在UE4中的C++编程·
- 【UE4官方文档翻译】Introduction to C++ Programming in UE4 (介绍UE4中的C++编程)
- Functional Programming函数式编程
- 函数式编程Functional Programming
- 函数编程(Functional programming)
- python 学习笔记8 函数式编程functional programming
- 函数式编程(Functional Programming)
- 函数式编程(functional programming)学习总结
- python函数式编程(Functional Programming)
- In-depth: Functional programming in C++ - 在C++上面使用函数式编程
- Introduction to Java Programming
- 编程思想基本概念之函数式编程(Functional Programming)
- Why you should embrace functional programming in Java 8
- 关于函数式编程(Functional Programming)的学习笔记Ⅰ
- 5.1.3. Functional Programming Tools(函数式编程工具)
- Introduction to Java Programming编程题9.7<将字符串中的字符转换为数字>
- iOS开发中的零碎知识点笔记 韩俊强的博客
- IBatis和Hibernate区别
- POJ 2728 Desert King 01分数规划 最优比率生成树
- 排序算法之归并算法
- 何为“游戏性”?
- 简要介绍JAVA8中的函数式编程;Brief Introduction to Functional Programming in Java 8
- C/CPP点滴积累--C、CPP函数定义的小差别
- 去掉ecshop中随机显示的 power by ecshop
- android studio查看logcat日志
- java学习日记——Object类II
- Redis集群方案及实现Sentienl
- 两个c程序测试机器性能
- python测试微信中谁把你删除了
- poj1102数字打印,七段数字码