久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何創建無限流<E>出迭代器<E&a

How to create an infinite Streamlt;Egt; out of an Iteratorlt;Egt;?(如何創建無限流lt;Egt;出迭代器lt;Egt;?)
本文介紹了如何創建無限流<E>出迭代器<E>?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

看看我制作的以下課程:

Looking at the following class I've made:

public class FibonacciSupplier implements Iterator<Integer> {
    private final IntPredicate hasNextPredicate;

    private int beforePrevious = 0;
    private int previous = 1;

    private FibonacciSupplier(final IntPredicate hasNextPredicate) {
        this.hasNextPredicate = hasNextPredicate;
    }

    @Override
    public boolean hasNext() {
        return hasNextPredicate.test(previous);
    }

    @Override
    public Integer next() {
        int result = beforePrevious + previous;
        beforePrevious = previous;
        previous = result;
        return result;
    }

    public static FibonacciSupplier infinite() {
        return new FibonacciSupplier(i -> true);
    }

    public static FibonacciSupplier finite(final IntPredicate predicate) {
        return new FibonacciSupplier(predicate);
    }
} 

以及它的用法:

public class Problem2 extends Problem<Integer> {
    @Override
    public void run() {
        result = toList(FibonacciSupplier.finite(i -> (i <= 4_000_000)))
                .stream()
                .filter(i -> (i % 2 == 0))
                .mapToInt(i -> i)
                .sum();
    }

    @Override
    public String getName() {
        return "Problem 2";
    }

    private static <E> List<E> toList(final Iterator<E> iterator) {
        List<E> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }
}

我怎樣才能創建一個infinite Stream<E>?

How would I be able to create an infinite Stream<E>?

如果我使用 Stream<Integer>infiniteStream = toList(FibonacciSupplier.infinite()).stream(),可能令人驚訝的是,我永遠不會得到無限流.
相反,代碼將在底層方法中創建 list 時永遠循環.

If I were to use Stream<Integer> infiniteStream = toList(FibonacciSupplier.infinite()).stream(), I would, possibly surprisingly, never get an infinite stream.
Instead the code would loop forever in the creation of the list in an underlying method.

到目前為止,這純粹是理論上的,但如果我想先跳過無限流中的前 x 個數字,然后將其限制為最后 y 個數字,我絕對可以理解它的必要性,例如:

This so far is purely theoretical, but I can definately understand the need for it if I would want to first skip the first x numbers from an infinite stream, and then limit it by the last y numbers, something like:

int x = MAGIC_NUMBER_X;
int y = MAGIC_NUMBER_y;
int sum = toList(FibonacciSupplier.infinite())
    .stream()
    .skip(x)
    .limit(y)
    .mapToInt(i -> i)
    .sum();

代碼永遠不會返回結果,應該怎么做?

The code would not ever return a result, how should it be done?

推薦答案

你的錯誤是認為你需要一個 Iterator 或一個 Collection 來創建一個 .對于創建無限流,一個方法提供一個接一個的值就足夠了.所以對于你的類 FibonacciSupplier 最簡單的用法是:

Your mistake is to think that you need an Iterator or a Collection to create a Stream. For creating an infinite stream, a single method providing one value after another is enough. So for your class FibonacciSupplier the simplest use is:

IntStream s=IntStream.generate(FibonacciSupplier.infinite()::next);

或者,如果您更喜歡裝箱的值:

or, if you prefer boxed values:

Stream<Integer> s=Stream.generate(FibonacciSupplier.infinite()::next);

請注意,在這種情況下,方法不必命名為 next 也不必滿足 Iterator 接口.但它是否與您的班級一樣并不重要.此外,由于我們剛剛告訴流使用 next 方法作為 Supplier,因此永遠不會調用 hasNext 方法.它只是無限的.

Note that in this case the method does not have to be named next nor fulfill the Iterator interface. But it doesn’t matter if it does as with your class. Further, as we just told the stream to use the next method as a Supplier, the hasNext method will never be called. It’s just infinite.

使用 Iterator 創建一個有限流有點復雜:

Creating a finite stream using your Iterator is a bit more complicated:

Stream<Integer> s=StreamSupport.stream(
  Spliterators.spliteratorUnknownSize(
    FibonacciSupplier.finite(intPredicate), Spliterator.ORDERED),
  false);

在這種情況下,如果您想要一個具有未裝箱 int 值的有限 IntStream,您的 FibonacciSupplier 應該實現 PrimitiveIterator.OfInt.

In this case if you want a finite IntStream with unboxed int values your FibonacciSupplier should implement PrimitiveIterator.OfInt.

這篇關于如何創建無限流&lt;E&gt;出迭代器&lt;E&gt;?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 欧美一区二区免费 | 91久久精品一区二区二区 | 欧美不卡 | 免费黄色av网站 | 中文字幕一区二区三区精彩视频 | 一区二区三区不卡视频 | 91久久夜色 | 中文成人无字幕乱码精品 | 乳色吐息在线观看 | 精品视频网 | 国产精品毛片一区二区三区 | 欧美视频| 国产乱码精品一区二区三区av | 91资源在线 | 国产欧美一区二区精品久导航 | 久久免费视频在线 | 成人性生交a做片 | 羞羞视频网站在线观看 | 久久99精品久久 | 亚洲三区在线观看 | 久久99精品久久久久久噜噜 | 欧美专区在线视频 | 日日射夜夜骑 | 毛色毛片免费看 | 国产丝袜一区二区三区免费视频 | 范冰冰一级做a爰片久久毛片 | 这里有精品 | 毛片免费在线观看 | 9191av| 婷婷色婷婷 | 成人黄色在线视频 | 一区二区三区国产好 | 日韩在线播放一区 | 91精品国产一区 | 日韩成人免费中文字幕 | 亚洲精品在线免费看 | 成人国产精品久久久 | 久久午夜国产精品www忘忧草 | www.黄色在线观看 | 五月天国产在线 | 一区二区久久精品 |