Stream Manipulation(java 8)

Asked

Viewed 138 times

1

Good morning guys I would like to get a little doubt, I have a String Stream with many lines of a txt, I need to do a split to break the lines in the Pipes, but the return as you know is a String vector, summarizing I would like to know if it is possible to get to this:

Stream<String> linhas=Files.lines(caminho,StandardCharsets.ISO_8859_1);
/**Alguma coisa aqui...**/
Stream<String[]> linhasSemPipe;//Resultado

Manipulating only Streams, no conversion to another type.

Thanks for your attention.

  • Show the input and output file contents you expect.

2 answers

1


You can use the method map.

Stream<String[]> linhasSemPipe =  Files.lines(caminho,StandardCharsets.ISO_8859_1)
                                       .map(linha -> linha.split("\\|"));

1

You can try something like this by returning a Stream:

Stream<String> linesByPipe = 
   Files.lines(caminho)
        .map(line -> line.split("\\|")) // Quebra por '|'

Or so, returning one List:

List<String> linesByPipe = 
   Files.lines(caminho)
        .map(line -> line.split("\\|")) // Quebra por '|' 
        .collect(Collectors.toList());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.