How to split list

45
June 13, 2019, at 4:00 PM

I've got an ArrayList which is filled from input file line by line. One line is one index here, so I need to split that by words because I dont want that. Can someone explain me how to do that? I prefer to use Lists not Arrays btw.

    Scanner data = new Scanner(new File("C:\\X\\X.txt"));
    Scanner data2 = new Scanner(new File("C:\\Y\\Y.txt"));
    List<String> file1 = new ArrayList<>(write1(data));
    List<String> file2 = new ArrayList<>(write2(data2));

Actual output is:

[Ian Kowalsky 2000, Pit Johnson 2001, Adam Todd 1999]

Expected:

[Ian, Kowalsky, 2000, Pit, Johnson, 2001, Adam, Todd, 1999]
Answer 1

If you need to create a new list in which the elements are the words from the previous list of phrases, you could do:

list.stream()                           // [Ian Kowalsky 2000, Pit Johnson 2001, Adam Todd 1999]
   .map(phrase -> phrase.split("\\s+")) // [[Ian, Kowalsky, 2000], [Pit, Johnson, 2001], [Adam, Todd, 1999]]
   .flatMap(Arrays::stream)             // [Ian, Kowalsky, 2000, Pit, Johnson, 2001, Adam, Todd, 1999]
   .collect(Collectors.toList());

Check the documentation for flatMap in here.

Answer 2

If you are using Java 8 you can use :

List<String> result = lines.stream()
        .flatMap(line -> Arrays.stream(line.split("\\s+")))
        .collect(Collectors.toList());
Answer 3

If you just want to split every line into words, and can use java8+, try this :

Load the lines via Files.lines and then simply use String.splitto explode your lines into words... Something like so :

List<String> words = Files.lines(file).flatMap(line -> Stream.of(line.split("\\s+"))).collect(Collectors.toList());
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WordsOfFiles {
    public static void main(String[] args) throws IOException {
        List<String> words = Files.lines(new File("X.txt").toPath()).flatMap(line -> Stream.of(line.split("\\s+"))).collect(Collectors.toList());
        words.forEach(System.out::println);
    }
}

HTH!

READ ALSO
@AfterClass method don&#39;t finish the testcase

@AfterClass method don't finish the testcase

I'm testing my databseI'm using the @BeforeClass method to delete everything in my databasetable then inserting some data

34
Android.graphics.rect - always null

Android.graphics.rect - always null

I want to write some tests for my Android project - and I‘m trying to instantiate a Rect()-Object in a JUnit test - but I’m always getting a null instead of the objectHere some examples:

45
Issue with parsing LocalDate to Date in Java [duplicate]

Issue with parsing LocalDate to Date in Java [duplicate]

This question already has an answer here:

59
Nonblocking Java Async Processing - how to constrain memory usage?

Nonblocking Java Async Processing - how to constrain memory usage?

I'm coming back to Java after a few years away, and have been excited to see the introduction of non-blocking async support in the new javanet

49