美文网首页
java函数编程笔记

java函数编程笔记

作者: cnzhanhao | 来源:发表于2018-10-14 23:57 被阅读80次
image.png

他的特性:
函数作为一等公民
无副作用
引用透明
申明式的(Declarative)
不变模式
易于并行
更少的代码
引用透明
易于阅读的

下面举几个例子吧:forEach,filter,map

forEach
Arrays.stream(new int[]{1,2,3,5}).forEach(x->System.out.println(x));

filter
static int[] arr={1,3,4,5,6,7,8,9,10};
public static void main(String[] args) {
Arrays.stream(arr).filter((x)->x%2==0).forEach(System.out::println);
}

map
static int[] arr={1,3,4,5,6,7,8,9,10};
public static void main(String[] args) {
Arrays.stream(arr).map((x)->x*x).forEach(System.out::println);
}

其他的使用方法,到时候查其他文档吧
https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/

不得不说函数编程在某些方面还是挺方便的,他和面向对象只能说分工不同吧。
面向对象可能更复杂的描述整个世界,函数编程在具体的计算问题上,是面向对象不能比的。

相关文章

网友评论

      本文标题:java函数编程笔记

      本文链接:https://www.haomeiwen.com/subject/sllxzftx.html