[技術分享]20171221_java8_創建stream
阿新 • • 發佈:2017-12-22
lec str reac team style rand body int 分享
public class StreamTest { //生成stream的四種方式: @Test public void test1(){ //1.第一種:通過collection集合提供的stream方法生成 List<String> list = Arrays.asList("1","2","3","4"); Stream<String> stream = list.stream(); stream.forEach(System.out::print);//2.第二種:通過Arrays提供的stream方法生成 String[] s = new String[]{"1","2","3","4"}; Stream<String> stream2 = Arrays.stream(s); stream2.forEach(System.out::print); //3.第三種:利用steam的靜態方法of Stream<String> stream3 = Stream.of("1","2","3","4"); stream3.forEach(System.out::print);//4.第四種:創建無限流 //選代流 Stream<Integer> stream4 = Stream.iterate(0,(x)->x+2); stream4.forEach(System.out::print); //生成 Stream<Double> stream5 = Stream.generate(Math::random); stream5.limit(5).forEach(System.out::print); } }
[技術分享]20171221_java8_創建stream