/java_in_examples

Source example for java

Primary LanguageJavaApache License 2.0Apache-2.0

This is collection of java code examples, best practice and useful article and link for Java

Java code examples and best practice

You can find following code examples:

Java Collections and Stream Api in Article

I. Stream API

1. All way to create Stream in Java 8
Way to create stream Template Example
  1. Classic: Create stream from collection | collection.stream() | Collection collection = Arrays.asList("a1", "a2", "a3");
    Stream streamFromCollection = collection.stream();
  2. Create stream from values | Stream.of(value1,… ,valueN) | Stream streamFromValues = Stream.of("a1", "a2", "a3");
  3. Create stream from array | Arrays.stream(array) | String[] array = {"a1","a2","a3"};
    Stream streamFromArrays = Arrays.stream(array);
  4. Create stream from part of array | Arrays.stream(array, start, end) | String[] array = {"a1","a2","a3"};
    Stream streamFromArrays = Arrays.stream(array, 1, 2);
  5. Create stream from file (every row from file become element of stream) | Files.lines(file_path) | Stream 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 stream = collection.parallelStream();
  9. Create infinive strean using Stream.iterate | Stream.iterate(init_value, generate_expression) | Stream streamFromIterate = Stream.iterate(1, n -> n + 1);
  10. Create infinive strean using Stream.generate | Stream.generate(generate_expression) | Stream streamFromGenerate = Stream.generate(() -> "a1");
  11. Create stream from path | Files.list(file_path) | Stream streamFromPath = Files.list(Paths.get(""));
  12. Create stream from finding files | Files.find(file_path, max_depth, mathcher) | Stream streamFromFind = Files.find(Paths.get(""), 10, (p,a) -> true);
  13. Create stream from files tree | Files.walk(file_path) | Stream 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 streamEmpty = Stream.empty();
  20. Create stream from Pattern | Pattern.compile(reg_exp).splitAsStream(string) | Stream streamFromPattern = Pattern.compile(":").splitAsStream("a1:a2:a3");
  21. Create stream from BufferedReader | bufferedReader.lines() | Stream streamFromBufferedReader = bufferedReader.lines();
  22. Create stream from Enum | EnumSet.allOf(MyEnum.class).stream() | Stream streamFromEnum = EnumSet.allOf(MyEnum.class).stream();

More examples this