Java 函數式程式設計在 Java 8th 版本之後出現。函數式程式設計意味著解決問題的方法發生了根本性的變化。函數式程式設計允許使用表達式(聲明函數)進行程式設計、將函數作為參數傳遞以及使用函數作為語句。
函數式程式設計的型別
廣告 該類別中的熱門課程 程式語言 - 專業化 | 54 課程系列 | 4 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
在討論函數式程式設計概念之前,讓我們先來看看函數式程式設計和結構化程式設計之間的差異。結構化程式設計強調邏輯結構或過程,而函數式程式設計主要關注資料。結構化程式設計遵循自上而下的方法,而函數式程式設計遵循自下而上的方法。
函數式程式設計分成稱為物件的微小運行時實體,而結構化程式設計則分為小單元或函數。結構化程式設計的安全性較低,而函數式程式設計的安全性很高。結構化程式設計無法處理複雜問題,而函數式程式設計可以處理任何程度的複雜問題。
下面給出了提到的範例:
文法:
objectName.stream();
代碼:
動物.java
package com.streams; public class Animal { String name; String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Animal(String name, String color) { super(); this.name = name; this.color = color; } }
AnimalNames.java
package com.streams;//creating package //importing required packages to the code import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class AnimalNames { public static void main(String[] args) { getMyDogNames();//calling the method } private static void getMyDogNames() { Animal a1=new Animal("Puppy","black"); //creating a object for animal class Animal a2=new Animal("Tommy","brown");//creating a object for animal class Animal a3=new Animal("Zimmy","white");//creating a object for animal class List<Animal> persons = Arrays.asList(a1,a2,a3); //passing object references to list String dogName = persons.stream() //covert all elements into stream .filter(name -> "Puppy".equals(name.getName()))//filtering given elements from persons class .map(Animal::getName) //persons elements iterate over map .findAny()//find out the given element passed from filter .orElse("");// System.out.println("Dog Name :" + dogName); //print the dog name List<String> collect = persons.stream() .map(Animal::getName) .collect(Collectors.toList());//collecting all names from list System.out.println("All Dog names"); collect.forEach(System.out::println);//iterating collection with for each loop } }
輸出:
說明:
文法:
(arguments) -> { //code for implementation } Arguments: argument-list can be have values or no values Example: arguments1, arguments2, arguments3,…… ->: Joins code implementation and arguments.
文法:
(argument1) -> { //code for implementation }
代碼:
package com.lambda;//creating a package interface Square{ //creating interface for quare public int getArea(int side); //create a method for get area } public class AreaOfSquare{ //As we are working with Lambda expression so no need to implement interface of square public static void main(String[] args) { Square area=(side)->{ // Lambda expression with only one argument. Return side*side; //returning area }; System.out.println(“Area of Square=>”+area.getArea(10)); //printing area by calling interface getArea method } }
輸出:
說明:
文法:
() -> { //code for implementation }
代碼:
package com.lambda;//creating a package interface Name{ //creating interface for Name public String getMyName(); //create a method for get name } public class MyNameString{ //As we are working with Lambda expression so no need to implement interface of Name public static void main(String[] args) { Name name=()->{ // Lambda expression with out argument. return "Hi, Amardeep!"; //returning name }; System.out.println(name.getMyName()); //printing name by calling interface getMyName method } }
輸出:
說明:
文法:
Class-Name:: static method name
代碼:
package method.reference;//creating package interface RectangleArea{ //creating RectangleArea interface public int getArea(int l,int b); //creating method getArea in interface } public class StaticMethodReference { //creating a classs public static int showRectangleArea(int length, int breadth){ //creating method for getting rectangle area return length*breadth; } public static void main(String[] args) { // Referring static method RectangleArea area = StaticMethodReference::showRectangleArea; //calling class name with method name // Calling interface method int outArea=area.getArea(10,20); System.out.println("Area of rectangle :"+outArea);//printing area } }
輸出:
說明:
函數式程式設計是透過流、lambda 表達式和方法引用來實現的。它減少了程式碼行並提高了效能。
以上是Java 中的函數式程式設計的詳細內容。更多資訊請關注PHP中文網其他相關文章!