Home / JAVA Complete Reference by Examples / Streams / How to split string into stream in Java 8 by Example
How to split string into stream in Java 8 by Example
19140 views.
package com.learnbyexamples.streams;

import java.util.regex.Pattern;
import java.util.stream.Stream;

public class E006_SplitStringIntoStream {
    public static void main(String[] args) {
        //Delimited names
        String names = "raja,siva,ram,kumar,vinoth";
        
        //Comma to split the string
        Pattern pattern = Pattern.compile(",");
        
        //Spliting names by comma converting into stream
        Stream<String> namesStream = pattern.splitAsStream(names);
        
        //Iterate stream using forEach and Print the names.
        namesStream.forEach(System.out::println);
    }
}
Output
raja
siva
ram
kumar
vinoth
Related Examples
   How to filter an List stream using Lambda expression in Java 8 by Example
   How to filter an Array stream using Lambda expression in Java 8 by Example
   How to filter an Map stream using Lambda expression in Java 8 by Example
   How to use Stream generate method using Lamdba expression in Java 8 Example
   How to read a file line by line using stream in Java 8 by Example
   How to split string into stream in Java 8 by Example
   How to transform stream elements using map in Java 8 by Example
   How to use flatMap stream method in Java 8 by Example
Copyright © 2016 Learn by Examples, All rights reserved