您好,登录后才能下订单哦!
Java8中怎么创建Stream流,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
一、使用数组
// 使用数组 String[] arr = { "ma", "zhi", "chu", "is", "java", "developer", "family" }; Stream<String> stream = Stream.of(arr);// stream = Stream.of("ma", "zhi", "chu", "is", "java", "developer", "family" ); String[] stringArr = {"ma", "zhi", "chu", "love"}; Stream<String> stream1 = Arrays.stream(stringArr);
二、使用Collections
// 使用Collections List<String> list = new ArrayList<String>(); list.add("ma"); list.add("zhi"); list.add("chu"); Stream<String> stream2 = list.stream();
三、使用Stream.generate()
// 使用Stream.generate() Stream<String> stream3 = Stream.generate(() -> "love").limit(10); String[] strArr3 = stream3.toArray(String[]::new); System.out.println(Arrays.toString(strArr3));
四、使用 Stream.iterate()
// 使用Stream.iterate() Stream<BigInteger> bigIntStream = Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE)).limit(10); BigInteger[] bigIntArr = bigIntStream.toArray(BigInteger[]::new); System.out.println(Arrays.toString(bigIntArr));
五、使用流行的APIs,如Pattern.compile().splitAsStream()
// 5、使用Popular APIs String sentence = "ma zhi chu is a Java wechat official account."; Stream<String> wordStream = Pattern.compile("\\W").splitAsStream(sentence); String[] wordArr = wordStream.toArray(String[]::new); System.out.println(Arrays.toString(wordArr));
看一下完整示例,
package com.mzc.common.stream; 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; /** * <p class="detail"> * 功能: Java8创建流的5种方法 * </p> * * @author Moore * @ClassName Create stream. * @Version V1.0. * @date 2019.12.02 11:55:24 */public class CreateStream { public static void main(String[] args) { // 1、使用数组 String[] arr = { "ma", "zhi", "chu", "is", "java", "developer", "family" }; Stream<String> stream = Stream.of(arr);// stream = Stream.of("ma", "zhi", "chu", "is", "java", "developer", "family" ); String[] stringArr = {"ma", "zhi", "chu", "love"}; Stream<String> stream1 = Arrays.stream(stringArr); System.out.println(Arrays.toString(stream.toArray(String[]::new))); System.out.println(Arrays.toString(stream1.toArray(String[]::new))); // 2、使用Collections List<String> list = new ArrayList<String>(); list.add("ma"); list.add("zhi"); list.add("chu"); Stream<String> stream2 = list.stream(); stream2.forEach(l->{ System.out.println(l); }); // 3、使用Stream.generate() Stream<String> stream3 = Stream.generate(() -> "love").limit(10); String[] strArr3 = stream3.toArray(String[]::new); System.out.println(Arrays.toString(strArr3)); // 4、使用Stream.iterate() Stream<BigInteger> bigIntStream = Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE)).limit(10); BigInteger[] bigIntArr = bigIntStream.toArray(BigInteger[]::new); System.out.println(Arrays.toString(bigIntArr)); // 5、使用Popular APIs String sentence = "ma zhi chu is a Java wechat official account."; Stream<String> wordStream = Pattern.compile("\\W").splitAsStream(sentence); String[] wordArr = wordStream.toArray(String[]::new); System.out.println(Arrays.toString(wordArr)); }}
关于Java8中怎么创建Stream流问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。