java函數(shù)式編程其實(shí)就是在一個(gè)接口中有一個(gè)抽象方法的接口,稱為函數(shù)式接口,當(dāng)然接口可以包含其他的方法,比如默認(rèn)、靜態(tài)、私有,那java函數(shù)式編程使用場(chǎng)景是什么?下面來(lái)我們就來(lái)給大家講解一下。
首先,我們先定義一個(gè)函數(shù)式編程接口
@FunctionalInterfacepublic interface BooleanFunctionalInterface { boolean test(T t); }
很簡(jiǎn)單,該接口的唯一一個(gè)抽象方法(并且非Object類的方法)返回值為boolean
下面,定義一個(gè)方法,接受一個(gè)List,利用實(shí)現(xiàn)了該接口的test方法的對(duì)象,篩選出需要的元素:
import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class Filter { public staticListfilter(Listlist, BooleanFunctionalInterface b) { if (CollectionUtils.isEmpty(list)) { return new ArrayList < > (0); } Listresult = new ArrayList < > (list.size()); for (int i = 0; i < list.size(); p = "" i++) { <= "" > T t = list.get(i); if (b.test(t)) { result.add(t); } } return result; } }
測(cè)試類,篩選出年齡大于25的People對(duì)象:
public class FunctionalInterfaceTest { private ListpeopleList = new ArrayList < > (); @Before public void init() { peopleList.add(new People("LuoTianyan", 23)); peopleList.add(new People("ff", 26)); peopleList.add(new People("Tony", 33)); } @Test public void testUserDefined() { Listfilter = Filter.filter(peopleList, p - > ((People) p) .getAge() > 25); filter.forEach(System.out::println); } }
import lombok.*; @Getter @Setter @ToString @AllArgsConstructor @NoArgsConstructorpublic class People { private String name; private int age; }
JDK中已有的函數(shù)式接口
上面的自定義的函數(shù)式接口,返回boolean,其實(shí)在Java8中已經(jīng)有該類型的接口,那就是Predicate。
Predicate
該接口定義了一個(gè)支持泛型的boolean test( T)的抽象方法,其函數(shù)描述符為 (T)->
boolean,現(xiàn)在我們就可以直接使用Predicate
在上面的Filter類中追加一個(gè)方法,修改這里形參為Predicate。
public staticListpredicate(Listlist, Predicate predicate) { if (CollectionUtils.isEmpty(list)) { return new ArrayList < > (0); } Listresult = new ArrayList < > (list.size()); for (int i = 0; i < list.size(); p = "" i++) { <= "" > T t = list.get(i); if (predicate.test(t)) { result.add(t); } } return result; }
獲取Age>25的People對(duì)象,測(cè)試如下:
@Test public void testPredicate() { Listpredicate = Filter.predicate(peopleList, p - > ((People) p) .getAge() > 25); predicate.forEach(System.out::println); }
Consumer
該接口定義了一個(gè)void accept(T)的抽象方法,其函數(shù)描述符為 (T) ->
void,如果你需要一個(gè)操作某一對(duì)象,但無(wú)需返回的的函數(shù)式接口,那么就可以使用Consumer
追加方法,形參為Consumer:
public staticvoid consumer(Listlist, Consumer consumer) { if (CollectionUtils.isEmpty(list)) { return; } Listresult = new ArrayList < > (list.size()); for (int i = 0; i < list.size(); p = "" i++) { <= "" > T t = list.get(i); consumer.accept(t); } }
下面實(shí)現(xiàn)修改所有Age為18,并且輸出,測(cè)試如下:
@Test public void testConsumer() { // setAge操作不需要返回值 ConsumersetAgeConsumer = p - > ((People) p) .setAge(18); Filter.consumer(peopleList, setAgeConsumer); // 輸出操作不需要返回值 Consumersout = p - > System.out.println((People) p); Filter.consumer(peopleList, sout); }
Supplier
既然有消費(fèi)者接口(Consumer
追加方法,形參Supplier
public staticListlistFactory(int count, Suppliersupplier) { Listresult = new ArrayList < > (count); for (int i = 0; i < count; p = "" i++) { <= "" > T t = supplier.get(); result.add(t); } return result; }
下面生成count個(gè)對(duì)象,設(shè)置對(duì)象默認(rèn)屬性值:
@Test public void testSupplier() { // 生成對(duì)象 SupplierpeopleSupplier = () - > new People("init", 18); Listpeople = Filter.listFactory(5, peopleSupplier); // 輸出操作不需要返回值 Consumersout = p - > System.out.println((People) p); Filter.consumer(people, sout); }
Function
該接口定義了一個(gè) R
apply(T)類型的抽象函數(shù),它接受一個(gè)泛型變量T,并返回一個(gè)泛型變量R,如果你需要將一個(gè)對(duì)象T映射成R,那么就可以使用Function
下面,我們將對(duì)象轉(zhuǎn)化為String類型的例子
public staticListfunction(Listlist, Functionfunction) { if (CollectionUtils.isEmpty(list)) { return new ArrayList < > (0); } Listresult = new ArrayList < > (list.size()); for (int i = 0; i < list.size(); p = "" i++) { <= "" > T t = list.get(i); String apply = function.apply(t); result.add(apply); } return result; }
將People對(duì)象,轉(zhuǎn)換為的字符串輸出:
@Test public void testFunction() { // 將People對(duì)象,轉(zhuǎn)換為如下形式的字符串 Functionfunction = (People p) - > "name:" + p.getName() + " , age:" + p.getAge(); Liststrings = Filter.function(peopleList, function); // 輸出操作不需要返回值 Consumersout = p - > System.out.println(p); Filter.consumer(strings, sout); }
上面代碼的優(yōu)化
由于上面的people是list集合,可以直接利用stream的形式;
比如People對(duì)象轉(zhuǎn)換成字符串
@Test
public void testListStream() { Listcollect = peopleList.stream() .map(p - > p.getName() + p.getAge()) .collect(Collectors.toList()); collect.forEach(System.out::println); }
java函數(shù)式編程能夠減少了可變量的聲明,進(jìn)行更好的利用并行,并且代碼會(huì)更加簡(jiǎn)潔和可讀,因此java函數(shù)式編程還是很有必要的。最后大家如果想要了解更多初識(shí)java知識(shí),敬請(qǐng)關(guān)注賦能網(wǎng)。
本文鏈接:
本文章“java函數(shù)式編程使用場(chǎng)景是什么?java函數(shù)式編程使用場(chǎng)景”已幫助 55 人
免責(zé)聲明:本信息由用戶發(fā)布,本站不承擔(dān)本信息引起的任何交易及知識(shí)產(chǎn)權(quán)侵權(quán)的法律責(zé)任!
本文由賦能網(wǎng) 整理發(fā)布。了解更多培訓(xùn)機(jī)構(gòu)》培訓(xùn)課程》學(xué)習(xí)資訊》課程優(yōu)惠》課程開班》學(xué)校地址等機(jī)構(gòu)信息,可以留下您的聯(lián)系方式,讓課程老師跟你詳細(xì)解答:
咨詢熱線:4008-569-579