Java8實戰(zhàn)之Stream的延遲計算
一、函數(shù)式編程
要被稱為函數(shù)式,函數(shù)或方法不應該拋出任何異常。使用Optional<R> 類型作為返回值。
透明性:方法中沒有任何操作會修改現(xiàn)有結(jié)構(gòu)
使用Java8進行編程時,盡量使用Stream取代迭代操作。如果遞歸可以更簡潔,且不帶副作用,應該使用遞歸替換迭代。
"尾-遞"迭代,不需要在不同的棧楨上保存每次遞歸計算的中間值。目前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)文章
Java SpringBoot實現(xiàn)文件上傳功能的示例代碼
這篇文章主要介紹了如何利用Java SpringBoot實現(xiàn)文件上傳功能,文中的示例代碼講解詳細,對我們學習有一定幫助,需要的可以參考一下2022-03-03
使用Java實現(xiàn)MySQL數(shù)據(jù)鎖定的策略
在并發(fā)環(huán)境下,多個線程同時對MySQL數(shù)據(jù)庫進行讀寫操作可能會導致數(shù)據(jù)沖突和不一致的問題,為了解決這些并發(fā)沖突,我們可以采用數(shù)據(jù)鎖定策略來保證數(shù)據(jù)的一致性和完整性,下面將介紹如何使用Java實現(xiàn)MySQL數(shù)據(jù)鎖定策略,,需要的朋友可以參考下2023-08-08
SpringBoot對Filter過濾器中的異常進行全局處理方案詳解
這篇文章主要介紹了SpringBoot對Filter過濾器中的異常進行全局處理,在SpringBoot中我們通過 @ControllerAdvice 注解和 @ExceptionHandler注解注冊了全局異常處理器,需要的朋友可以參考下2023-09-09
Netty分布式pipeline管道創(chuàng)建方法跟蹤解析
這篇文章主要為大家介紹了Netty分布式pipeline管道創(chuàng)建方法跟蹤解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03

