Skip to content

Instantly share code, notes, and snippets.

@hadinajafi
Last active December 20, 2020 17:21
Show Gist options
  • Save hadinajafi/7e45afe7f42d4e215e2136ae37f6772c to your computer and use it in GitHub Desktop.
Save hadinajafi/7e45afe7f42d4e215e2136ae37f6772c to your computer and use it in GitHub Desktop.
Overall Java 8 Streams code samples
// break a string into multiple substrings with RegEx
Stream<String> streamOfString = Pattern.compile(", ").splitAsStream("a, b, c");
//creating stream of strings from array
String[] arr = {"a", "b", "c"};
Stream<String> streamOfArray = Arrays.stream(arr);
// every line of text becomes an element of stream
Path path = Paths.get("C:\\file.txt");
Stream<String> streamOfStrings = Files.lines(path);
Stream<String> streamWithCharset = Files.lines(path, Charset.forName("UTF-8"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment