8种方法在java8中创建流对象(Stream)

8种方法在java8中创建流对象(Stream)

 

 

  1. 创建空的Stream

empty()方法用来创建一个空的流

Stream<String> stream = Stream.empty();

stream.forEach(System.out::println);

 

这个方法经常被用在创建一个不含元素的空的流进行返回,避免返回null

public Stream<String> streamOf(List<String> list) {return list == null || list.isEmpty() ? Stream.empty() : list.stream();}

 

 

  1. 从集合创建Stream

一个流对象可以用Collection类型(Collection,List,Set)创建

 

import java.io.IOException;import java.util.Arrays;import java.util.Collection;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.stream.Stream;

 
public class StreamCreationExamples {public static void main(String[] args) throws IOException {Collection<String> collection = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");Stream<String> stream2 = collection.stream();stream2.forEach(System.out::println);List<String> list = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");Stream<String> stream3 = list.stream();stream3.forEach(System.out::println);Set<String> set = new HashSet<>(list);Stream<String> stream4 = set.stream();stream4.forEach(System.out::println);}}

输出:

JAVAJ2EESpringHibernateJAVAJ2EESpringHibernateJAVAHibernateJ2EESpring

 

 

  1. 从数组创建流对象

可以创建一个流对象从一个数组或者部分数组

 

import java.io.IOException;import java.util.Arrays;import java.util.stream.Stream;
public class StreamCreationExamples {public static void main(String[] args) throws IOException {// 数组作为Stream源Stream<String> streamOfArray = Stream.of("a", "b", "c");streamOfArray.forEach(System.out::println);// 从数组或者数组的部分元素中创建流String[] arr = new String[] { "a", "b", "c" };Stream<String> streamOfArrayFull = Arrays.stream(arr);streamOfArrayFull.forEach(System.out::println);Stream<String> streamOfArrayPart = Arrays.stream(arr, 1, 3);streamOfArrayPart.forEach(System.out::println);}}

输出:

abcabcbc

 

 

 

  1. 使用Stream.builder()

 

当使用builder()的时候,要声明希望存放的元素类型,此方法就会创建一个流对象

Stream<String> streamBuilder = Stream.<String>builder().add("a").add("b").add("c").build();
streamBuilder.forEach(System.out::println);

输出:

abc

 

 

 

  1. 使用Stream.generate()

 

Stream<String> streamGenerated = Stream.generate(() -> "element").limit(10);
streamGenerated.forEach(System.out::println);

输出:

elementelementelementelementelement

 

 

  1. 使用Stream.iterate()

 

Stream<Integer> streamIterated = Stream.iterate(1, n -> n + 2).limit(5);
streamIterated.forEach(System.out::println);

输出:

13579

 

 

 

  1. 从文件获取流

可以使用lines()方法从nio的类,文件生成一个文本流,每一行都会生成一个流对象

import java.io.IOException;import java.nio.charset.Charset;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.stream.Stream;

 
public class StreamCreationExamples {
    public static void main(String[] args) throws IOException {

 
        Path path = Paths.get("C:\\file.txt");
        Stream<String> streamOfStrings = Files.lines(path);
        Stream<String> streamWithCharset = Files.lines(path, Charset.forName("UTF-8"));
        streamOfStrings.forEach(System.out::println);
        streamWithCharset.forEach(System.out::println);
        streamOfStrings.close();
        streamWithCharset.close();
    }
}

 

  1. 从原始数据中获取

 

 

import java.io.IOException;

import java.util.Random;

import java.util.stream.DoubleStream;

import java.util.stream.IntStream;

import java.util.stream.LongStream;

 

public class StreamCreationExamples {

    public static void main(String[] args) throws IOException {

       IntStream intStream = IntStream.range(1, 3);

       intStream.forEach(System.out::println);

 

       LongStream longStream = LongStream.rangeClosed(1, 3);

       longStream.forEach(System.out::println);

 

       Random random = new Random();

       DoubleStream doubleStream = random.doubles(3);

       doubleStream.forEach(System.out::println);

   }

}

 

Output:

1

2

1

2

3

0.6929414814363383

0.3683384343302385

0.948888698350225

 

 

原文链接来自于:

 

https://dzone.com/articles/8-ways-of-creating-a-stream-object-in-java-8

第一次翻译文章,原本准备是给spring4all的,只是忘记了,只能发自己的博客了,唉

 

    原文作者:长河
    原文地址: https://blog.csdn.net/u010398771/article/details/83303536
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞