package com.shrimpking.t3;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/9/11 14:25
*/
public class CreateStream
{
public static void main(String[] args)
{
List<Integer> list = Arrays.asList(1,3,5,7,9);
//从集合创建流
Stream<Integer> stream1 = list.stream();
stream1.forEach(x -> System.out.printf("%d ",x));
System.out.println();
int[] intArr = new int[]{2,4,6,8,10};
//从数据创建流
IntStream stream2 = Arrays.stream(intArr);
stream2.forEach(x -> System.out.printf("%d ",x));
System.out.println();
//由值创建流
Stream<String> stream3 = Stream.of("one","two","three");
stream3.forEach(x -> System.out.printf("%s ",x));
System.out.println();
//创建无限流
Stream<Integer> stream4 = Stream.iterate(0,x -> x + 2);
stream4.limit(5).forEach(x -> System.out.printf("%d ",x));
System.out.println();
//生成随机数无限流
Stream<Double> stream6 = Stream.generate(()-> Math.random());
stream6.limit(5).forEach(x -> System.out.printf("%f ",x));
System.out.println();
//从1到5,创建整数流
IntStream stream7 = IntStream.range(1,5);
stream7.forEach(x-> System.out.printf("%d ",x));
System.out.println();
//从1到5 创建浮点数流
LongStream stream8 = LongStream.rangeClosed(1,5);
stream8.forEach(x -> System.out.printf("%d ",x));
System.out.println();
}
}
02-27
536
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-09
551
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)