Created
June 27, 2021 14:17
-
-
Save pablohdzvizcarra/b458eea4d4d8c5e2a636db82163db443 to your computer and use it in GitHub Desktop.
show differents ways to create a stream with some different types in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pablohdz.application; | |
import com.pablohdz.entities.Person; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Optional; | |
import java.util.regex.Pattern; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class DifferentWaysToCreateStream | |
{ | |
public static void withArray(Person p01, Person p02, Person p03, Person p04, Person p05) | |
{ | |
Person[] people = {p01, p02, p03, p04, p05}; | |
long count = Stream.of(people).count(); | |
System.out.println("Count = " + count); | |
Arrays.stream(people).forEach(System.out::println); | |
} | |
public static void withFileText() | |
{ | |
Path path = Path.of("src/main/resources/first-names.txt"); | |
try (Stream<String> lines = Files.lines(path)) | |
{ | |
long count = lines.count(); | |
System.out.println(count); | |
} catch (IOException ioException) | |
{ | |
ioException.printStackTrace(); | |
} | |
} | |
public static void withRegexExpression() | |
{ | |
String sentence = "the quick brown fox jumps over the lazy dog"; | |
String[] words = sentence.split(" "); | |
Stream<String> wordsStream = Arrays.stream(words); | |
long count = wordsStream.count(); | |
System.out.println("Count = " + count); | |
//with pattern not create a any intermediate structure, is best in performance | |
Pattern pattern = Pattern.compile(" "); | |
long count1 = pattern.splitAsStream(sentence).count(); | |
System.out.println("Count = " + count1); | |
} | |
public static void streamFromString() | |
{ | |
String sentence = "the quick brown fox jumps over the lazy dog"; | |
sentence | |
.chars() | |
.mapToObj((int codePoint) -> Character.toString(codePoint)) | |
.filter((String letter) -> !letter.equals(" ")) | |
.distinct() | |
.sorted() | |
.forEach(System.out::print); | |
} | |
public static void skippingAndLimiting() | |
{ | |
IntStream | |
.range(0, 30) | |
.skip(10) | |
.limit(10) | |
.forEach(System.out::println); | |
Path path = Path.of("src/main/resources/first-names.txt"); | |
try (Stream<String> lines = Files.lines(path)) | |
{ | |
lines | |
.skip(10) | |
.filter((String name) -> name.startsWith("A")) | |
.forEach(System.out::println); | |
} catch (IOException ioException) | |
{ | |
ioException.printStackTrace(); | |
} | |
} | |
public static void dropWhileTakeWhile() | |
{ | |
Class<?> clzz = ArrayList.class; | |
clzz.getSuperclass(); | |
Stream.<Class<?>>iterate(clzz, aClass -> aClass.getSuperclass()) | |
.takeWhile(aClass -> aClass != null) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment