Skip to content

Instantly share code, notes, and snippets.

@sorokod
Created February 5, 2018 22:21
Show Gist options
  • Save sorokod/818ccd65cad967cdecb7dbf4006e4fa0 to your computer and use it in GitHub Desktop.
Save sorokod/818ccd65cad967cdecb7dbf4006e4fa0 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import static java.nio.file.Files.lines;
import static java.nio.file.Paths.get;
import static java.util.stream.Collectors.toList;
public class FileLineByLine {
private static <T> List<T> file2T(String fileName, Function<String, T> transform) throws IOException {
try (Stream<String> stream = lines(get(fileName))) {
return stream
.map(line -> transform.apply(line))
.collect(toList());
}
}
public static void main(String args[]) throws Exception {
List<Double> doubles = file2T(args[0], Double::parseDouble);
System.out.println(doubles);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment