<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [隨機數流](https://lingcoder.gitee.io/onjava8/#/book/14-Streams?id=%e9%9a%8f%e6%9c%ba%e6%95%b0%e6%b5%81) `Random`類被一組生成流的方法增強了。代碼示例: ~~~ // streams/RandomGenerators.java import java.util.*; import java.util.stream.*; public class RandomGenerators { public static <T> void show(Stream<T> stream) { stream .limit(4) .forEach(System.out::println); System.out.println("++++++++"); } public static void main(String[] args) { Random rand = new Random(47); show(rand.ints().boxed()); show(rand.longs().boxed()); show(rand.doubles().boxed()); // 控制上限和下限: show(rand.ints(10, 20).boxed()); show(rand.longs(50, 100).boxed()); show(rand.doubles(20, 30).boxed()); // 控制流大小: show(rand.ints(2).boxed()); show(rand.longs(2).boxed()); show(rand.doubles(2).boxed()); // 控制流的大小和界限 show(rand.ints(3, 3, 9).boxed()); show(rand.longs(3, 12, 22).boxed()); show(rand.doubles(3, 11.5, 12.3).boxed()); } } ~~~ 輸出結果: ~~~ -1172028779 1717241110 -2014573909 229403722 ++++++++ 2955289354441303771 3476817843704654257 -8917117694134521474 4941259272818818752 ++++++++ 0.2613610344283964 0.0508673570556899 0.8037155449603999 0.7620665811558285 ++++++++ 16 10 11 12 ++++++++ 65 99 54 58 ++++++++ 29.86777681078574 24.83968447804611 20.09247112332014 24.046793846338723 ++++++++ 1169976606 1947946283 ++++++++ 2970202997824602425 -2325326920272830366 ++++++++ 0.7024254510631527 0.6648552384607359 ++++++++ 6 7 7 ++++++++ 17 12 20 ++++++++ 12.27872414236691 11.732085449736195 12.196509449817267 ++++++++ ~~~ 為了消除冗余代碼,我創建了一個泛型方法`show(Stream<T> stream)`(在講解泛型之前就使用這個特性,確實有點作弊,但是回報是值得的)。類型參數`T`可以是任何類型,所以這個方法對**Integer**、**Long**和**Double**類型都生效。但是**Random**類只能生成基本類型**int**,**long**,**double**的流。幸運的是,`boxed()`流操作將會自動地把基本類型包裝成為對應的裝箱類型,從而使得`show()`能夠接受流。 我們可以使用**Random**為任意對象集合創建**Supplier**。如下是一個文本文件提供字符串對象的例子。 Cheese.dat 文件內容: ~~~ // streams/Cheese.dat Not much of a cheese shop really, is it? Finest in the district, sir. And what leads you to that conclusion? Well, it's so clean. It's certainly uncontaminated by cheese. ~~~ 我們通過**File**類將 Cheese.dat 文件的所有行讀取到`List<String>`中。代碼示例: ~~~ // streams/RandomWords.java import java.util.*; import java.util.stream.*; import java.util.function.*; import java.io.*; import java.nio.file.*; public class RandomWords implements Supplier<String> { List<String> words = new ArrayList<>(); Random rand = new Random(47); RandomWords(String fname) throws IOException { List<String> lines = Files.readAllLines(Paths.get(fname)); // 略過第一行 for (String line : lines.subList(1, lines.size())) { for (String word : line.split("[ .?,]+")) words.add(word.toLowerCase()); } } public String get() { return words.get(rand.nextInt(words.size())); } @Override public String toString() { return words.stream() .collect(Collectors.joining(" ")); } public static void main(String[] args) throws Exception { System.out.println( Stream.generate(new RandomWords("Cheese.dat")) .limit(10) .collect(Collectors.joining(" "))); } } ~~~ 輸出結果: ~~~ it shop sir the much cheese by conclusion district is ~~~ 在這里可以看到`split()`更復雜的運用。在構造器里,每一行都被`split()`通過方括號內的空格或其它標點符號分割。在方括號后面的`+`表示`+`前面的東西可以出現一次或者多次。 你會發現構造函數使用命令式編程(外部迭代)進行循環。在以后的例子中,你會看到我們是如何去除命令式編程的使用。這種舊的形式雖不是特別糟糕,但使用流會讓人感覺更好。 在`toString()`和`main()`方法中你看到了`collect()`操作,它根據參數來結合所有的流元素。當你用`Collectors.joining()`作為`collect()`的參數時,將得到一個`String`類型的結果,該結果是流中的所有元素被`joining()`的參數隔開。還有很多不同的`Collectors`用于產生不同的結果。 在主方法中,我們提前看到了**Stream.**`generate()`的用法,它可以把任意`Supplier<T>`用于生成`T`類型的流。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看