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

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

 

0. 创建空的Stream

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

Stream stream = Stream.empty();

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

 

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

public Stream streamOf(List 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 collection = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
    Stream stream2 = collection.stream();
    stream2.forEach(System.out::println);
    List list = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
    Stream stream3 = list.stream();
    stream3.forEach(System.out::println);
    Set set = new HashSet<>(list);
    Stream 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 streamOfArray = Stream.of("a", "b", "c");
streamOfArray.forEach(System.out::println);
// 从数组或者数组的部分元素中创建流
String[] arr = new String[] { "a", "b", "c" };
Stream streamOfArrayFull = Arrays.stream(arr);
streamOfArrayFull.forEach(System.out::println);
Stream streamOfArrayPart = Arrays.stream(arr, 1, 3);
streamOfArrayPart.forEach(System.out::println);
}
}
输出:
abcabcbc

 

  1. 使用Stream.builder()

 

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

Stream streamBuilder = Stream.builder().add("a").add("b").add("c").build();
streamBuilder.forEach(System.out::println);
输出:abc

 

  1. 使用Stream.generate()
Stream streamGenerated = Stream.generate(() -> "element").limit(10);

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

输出:

elementelementelementelementelement

 

  1. 使用Stream.iterate()
Stream 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 streamOfStrings = Files.lines(path);
        Stream streamWithCharset = Files.lines(path, Charset.forName("UTF-8"));
        streamOfStrings.forEach(System.out::println);
        streamWithCharset.forEach(System.out::println);
        streamOfStrings.close();
        streamWithCharset.close();
    }
}


始数据中获取

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的,只是忘记了,只能发自己的博客了,唉

 

你可能感兴趣的:(Scala)