Java8 已然到來,最大的變動就是Lambda. lambda中的stream是處理集合的源頭,下面簡單介紹一下建立stream的五種方式php
一、經過數組建立java
String[] arr = { "Java", "lambda", "test", "stream", "java", "web", "program" }; stream = Stream.of(arr);
stream = Stream.of("Java", "lambda", "test", "stream", "java", "web", "program");
String[] stringArr = {"a", "b", "c", "d"}; Stream<String> stream = Arrays.stream(stringArr);
二、經過集合來建立python
// from collection List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); stream = list.stream();
三、經過generate方法來建立web
Stream<String> stream = Stream.generate(() -> "test").limit(10); String[] strArr = stream.toArray(String[]::new); System.out.println(Arrays.toString(strArr));
四、經過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));
五、經過流行的APIcode
String sentence = "Lambda is a coming now."; Stream<String> wordStream = Pattern.compile("\\W").splitAsStream(sentence); String[] wordArr = wordStream.toArray(String[]::new); System.out.println(Arrays.toString(wordArr));
以上五種方式用來建立你的stream,functional 時代已然到來,擁抱lambda吧,java coder...string