Back to Useful Java Links

Readme

helloworlds/1.6-usefull-libraries/functional_programming/jdk_stream_api/readme.md

latest4.1 KB
Original Source
I. All way to create Stream in Java 8
Way to create streamTemplateExample
  1. Classic: Create stream from collection | collection.stream() | Collection<String> collection = Arrays.asList("a1", "a2", "a3");
    Stream<String> streamFromCollection = collection.stream();
  2. Create stream from values | Stream.of(value1,… ,valueN) | Stream<String> streamFromValues = Stream.of("a1", "a2", "a3");
  3. Create stream from array | Arrays.stream(array) | String[] array = {"a1","a2","a3"};
    Stream<String> streamFromArrays = Arrays.stream(array);
  4. Create stream from part of array | Arrays.stream(array, start, end) | String[] array = {"a1","a2","a3"}; Stream<String> streamFromArrays = Arrays.stream(array, 1, 2);
  5. Create stream from file (every row from file become element of stream) | Files.lines(file_path) | Stream<String> streamFromFiles = Files.lines(Paths.get("file.txt"));
  6. Create stream from stirng (every char become element of stream) | "string".chars() | IntStream streamFromString = "123".chars();
  7. Using Stream.builder | Stream.builder().add(...)....build() | Stream.builder().add("a1").add("a2").add("a3").build();
  8. Create parallel stream from collection | collection.parallelStream() | Stream<String> stream = collection.parallelStream();
  9. Create infinive strean using Stream.iterate | Stream.iterate(init_value, generate_expression) | Stream<Integer> streamFromIterate = Stream.iterate(1, n -> n 1);
  10. Create infinive strean using Stream.generate | Stream.generate(generate_expression) | Stream<String> streamFromGenerate = Stream.generate(() -> "a1");
  11. Create stream from path | Files.list(file_path) | Stream<Path> streamFromPath = Files.list(Paths.get(""));
  12. Create stream from finding files | Files.find(file_path, max_depth, mathcher) | Stream<Path> streamFromFind = Files.find(Paths.get(""), 10, (p,a) -> true);
  13. Create stream from files tree | Files.walk(file_path) | Stream<Path> streamFromFileTree = Files.walk(Paths.get(""));
  14. Create stream from all entities of jar file | new JarFile(jar_file).stream() | …
  15. Create stream from all entities of zip file | new ZipFile(zip_file).stream() | …
  16. Create stream from iterator | StreamSupport.stream(Spliterators. spliteratorUnknownSize(iterator, 0), false) | ...
  17. Create stream from iterable | StreamSupport.stream(iterable.spliterator(), false) | …
  18. Create infinive stream from iterator | Stream.generate(iterator::next) | …
  19. Create empty stream | Stream.empty() | Stream<String> streamEmpty = Stream.empty();
  20. Create stream from Pattern | Pattern.compile(reg_exp).splitAsStream(string) | Stream<String> streamFromPattern = Pattern.compile(":").splitAsStream("a1:a2:a3");
  21. Create stream from BufferedReader | bufferedReader.lines() | Stream<String> streamFromBufferedReader = bufferedReader.lines();
  22. Create stream from Enum | EnumSet.allOf(MyEnum.class).stream() | Stream<MyEnum> streamFromEnum = EnumSet.allOf(MyEnum.class).stream();
II. Using filter, findFirst, findAny, skip, limit и count

Collection: Arrays.asList("a1", "a2", "a3", "a1")

TaskExampleResult
Return count the number of occurrences "a1"collection.stream().filter("a1"::equals).count()2
Return first element of collection or "0", if collection is emptycollection.stream().findFirst().orElse(0)a1
Return last element of collection or "empty", if collection is emptycollection.stream().skip(collection.size() - 1).findAny().orElse("empty")a1
Find element == "a3" or throws exeptioncollection.stream().filter("a3"::equals).findFirst().get()a3
Return the third element from collectioncollection.stream().skip(2).findFirst().get()a3
Return two elements starting from secondcollection.stream().skip(1).limit(2).toArray()[a2, a3]
Return all elements according templatecollection.stream().filter((s) -> s.contains("1")).collect(Collectors.toList())[a1, a1]