Java8實戰(zhàn)之Stream的延遲計算
一、函數(shù)式編程
要被稱為函數(shù)式,函數(shù)或方法不應(yīng)該拋出任何異常。使用Optional<R> 類型作為返回值。
透明性:方法中沒有任何操作會修改現(xiàn)有結(jié)構(gòu)
使用Java8進(jìn)行編程時,盡量使用Stream取代迭代操作。如果遞歸可以更簡潔,且不帶副作用,應(yīng)該使用遞歸替換迭代。
"尾-遞"迭代,不需要在不同的棧楨上保存每次遞歸計算的中間值。目前Java不支持這種優(yōu)化,很多的現(xiàn)代JVM語言,比如Scala和Groovy都支持這種形式遞歸迭代的優(yōu)化。
1.1 示例一:方法中沒有任何操作會修改現(xiàn)有結(jié)構(gòu)
獲取列表的子集:
public static List<List<Integer>> findAllSubList(List<Integer> list) { if (list.size()==0) { List<List<Integer>> res = new ArrayList<>(); res.add(Collections.emptyList()); return res; } Integer first = list.get(0); List<Integer> subList = list.subList(1, list.size()); List<List<Integer>> allSubList = findAllSubList(subList); List<List<Integer>> allSubList2 = insertAll(first, allSubList); return concat(allSubList, allSubList2); } private static List<List<Integer>> concat(List<List<Integer>> allSubList, List<List<Integer>> allSubList2) { List<List<Integer>> res = new ArrayList<>(allSubList); res.addAll(allSubList2); return res; } private static List<List<Integer>> insertAll(Integer item, List<List<Integer>> allSubList) { List<List<Integer>> res = new ArrayList<>(); for (List<Integer> a : allSubList) { List<Integer> oneList = new ArrayList<>(a); oneList.add(item); res.add(oneList); } return res; }
1.2 實例二:“尾-遞”迭代
求n的階乘:
方案一:迭代
/** * 使用迭代計算階乘 * r 和 i 在每輪迭代中都會更新 * @param n * @return */ public static int factorialIterator(int n) { int r = 1; for (int i=1; i<=n; i++) { r *= i; } return r; }
方案二:使用遞歸
/** * 使用遞歸 計算階乘 * 比迭代都效率差:因為每次遞歸都需要創(chuàng)建棧楨 * @param n * @return */ public static int factorialRecursive(int n) { return n==1? 1 : n * factorialIterator(n-1); }
方案三:使用Stream
/** * 使用Stream 計算階乘 * @param n * @return */ public static int factorialStream(int n) { return IntStream.rangeClosed(1, n).reduce(1, (x, y)->x*y); }
方案四:遞歸的優(yōu)化:“尾-遞”迭代
/** * 尾-遞 迭代 * @param n * @return */ public static int factorialTailIterator(int n) { return factorialTailHelp(1, n); } /** * 尾-遞 迭代遞幫助類 * @param acc * @param n * @return */ private static int factorialTailHelp(int acc, int n) { return n==1?acc:factorialTailHelp(acc*n, n-1); }
二、科里化
科里化:幫助你模塊化函數(shù),提高代碼重用性的技術(shù)。
科里化表示一種將一個帶有n元組參數(shù)的函數(shù)轉(zhuǎn)換成n個一元函數(shù)鏈的方法。
三、函數(shù)式數(shù)據(jù)結(jié)構(gòu)——持久化的
數(shù)據(jù)結(jié)構(gòu)的值始終保持一致,不受其他部分變化的影響。
附加條件:所有使用持久化數(shù)據(jù)結(jié)構(gòu)的用戶都必須遵守這一“不修改“原則。不對返回值就行修改。
四、Stream的延遲計算
創(chuàng)建一個質(zhì)數(shù)列表:
4.1 列表接口
/** * @Date 2021/9/5 * @Author lifei */ public interface MyList<T> { T head(); MyList<T> tail(); MyList<T> filter(Predicate<T> p); default boolean isEmpty() { return true; } }
4.2 延遲列表
public class LazyList<T> implements MyList<T> { final T head; final Supplier<MyList<T>> tail; public LazyList(T head, Supplier<MyList<T>> tail) { this.head = head; this.tail = tail; } @Override public T head() { return head; } @Override public MyList<T> tail() { return tail.get(); } @Override public MyList<T> filter(Predicate<T> p) { return isEmpty()?this:p.test(head())? new LazyList<>(head, ()->tail().filter(p)):tail().filter(p); } @Override public boolean isEmpty() { return false; } }
4.3 創(chuàng)建一個無限延遲的列表
/** * 創(chuàng)建一個無限延遲的列表 * @param n * @return */ public static LazyList<Integer> from(int n) { return new LazyList<>(n, ()->from(n+1)); }
4.4 創(chuàng)建一個無限延遲的質(zhì)數(shù)列表
/** * 創(chuàng)建一個無限循環(huán)的 質(zhì)數(shù)列表 * @param numbers * @return */ public static MyList<Integer> primes(MyList<Integer> numbers) { return new LazyList<>(numbers.head(), ()->primes(numbers.tail().filter(n->n%numbers.head()!=0))); }
4.5 使用無限延遲的質(zhì)數(shù)列表
public static void main(String[] args) { LazyList<Integer> numbers = from(2); Integer res2 = numbers.head(); Integer res3 = numbers.tail().head(); Integer res4 = numbers.tail().tail().head(); System.out.println(res2); System.out.println(res3); System.out.println(res4); System.out.println("創(chuàng)建一個無限延遲的質(zhì)數(shù)列表"); MyList<Integer> primes = primes(numbers); for (int i=0; i<30; i++) { if (!primes.isEmpty()){ System.out.print(primes.head() + ", "); primes = primes.tail(); } } System.out.println(); }
總結(jié)
到此這篇關(guān)于Java8實戰(zhàn)之Stream延遲計算的文章就介紹到這了,更多相關(guān)Java8 Stream延遲計算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于IDEA配置Hibernate中遇到的問題解決
這篇文章主要給大家介紹了關(guān)于IDEA配置Hibernate中遇到的問題,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05詳解idea文件右鍵創(chuàng)建New沒有Create New Servlet的解決辦法
這篇文章主要介紹了詳解idea文件右鍵創(chuàng)建New沒有Create New Servlet的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Mybatis游標(biāo)查詢大量數(shù)據(jù)方式
這篇文章主要介紹了Mybatis游標(biāo)查詢大量數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02java 學(xué)習(xí)筆記(入門篇)_java的基礎(chǔ)語法
從基礎(chǔ)語法開始,這個語法你也可以理解為英語或是漢語里面的語法,只不過大家各有各的特點和區(qū)別;那么在學(xué)習(xí)的過程中我們就要不斷的積累重要的類和方法,這樣寫程序就會方便快捷了,下面就開始學(xué)習(xí)java的基礎(chǔ)語法2013-01-01