在Java8里创建Stream的几种方法

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
创建Stream的方法共有5种,分别为如下
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;
 
/**
 *
 * @author Kangjun Ways of Creating a Stream in Java 8
 */
public class StreamDemo {
 
    public static void main(String[] args) {
 
        arraysStream();
 
        collectionStream();
 
        generate();
 
        iterateStream();
         
        populaStream();
    }
 
    //From Arrays
    public static void arraysStream() {
        String[] arr = {"program", "creek", "program", "creek", "java", "web",
            "program"};
        Stream<String> stream = Stream.of(arr);
        System.out.println(Arrays.toString(arr));
 
    }
 
    //From Collections
    public static void collectionStream() {
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("python");
        list.add("c++");
        list.add("c");
        list.add("lisp");
 
        Stream<String> stream = list.stream().filter(p -> p.length() > 3);
        String[] arr = stream.toArray(String[]::new);
        System.out.println(Arrays.toString(arr));
    }
 
    //Use Stream.generate()
    public static void generate() {
        Stream<String> stream = Stream.generate(() -> "test").limit(10);
        String[] strArr = stream.toArray(String[]::new);
        System.out.println(Arrays.toString(strArr));
    }
 
    //Use Stream.iterate()
    public static void iterateStream() {
        Stream<BigInteger> bigIntStream = Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.TEN)).limit(10);
        BigInteger[] bigIntArr = bigIntStream.toArray(BigInteger[]::new);
        System.out.println(Arrays.toString(bigIntArr));
    }
 
    // From Popular APIs
    public static void populaStream() {
        String sentence = "Program creek is a Java site.";
        Stream<String> wordStream = Pattern.compile("\\W").splitAsStream(sentence);
        String[] wordArr = wordStream.toArray(String[]::new);
        System.out.println(Arrays.toString(wordArr));
    }
}

标签: isp

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:php使用ZipArchive压缩打包文件

下一篇:php验证邮箱格式