[JAVA] Stream type

Stream type

Let's take a look at the java.util.stream package added to Java 8. https://www.developer.com/java/data/stream-operations-supported-by-the-java-streams-api.html


Common methods that can be used in all streams are defined in Base Stream and Stream is a stream that processes object elements, IntStream, LongStream, and DoubleStream are streams that process basic types of int, long, and double elements, respectively.

Get a stream from a collection

List<String> list = Arrays.asList("Ah", "I", "U");
Stream<String> stream = list.stream(); // Collection . stream()

Get a stream from an array

String[] strArray = {"Ah", "I", "U"};
Stream<String> stream = Arrays.stream(strArray); 
int[] intArray = {1, 2, 3, 4};
IntStream intStream = Arrays.stream(intArray); 

Get a stream from a range of numbers

IntStream stream = IntStream.rangeClosed(1, 100);

Get stream from file

public static void main(String[] args) throws IOException{
        Path path = Paths.get("src/stream/list.txt");

        Stream<String> stream = Files.lines(path, Charset.defaultCharset()); 
        stream.forEach(System.out :: println);

        File file = path.toFile();
        FileReader fileReader = new FileReader(file);
        BufferedReader br = new BufferedReader(fileReader);
        stream = br.lines();
        stream.forEach(System.out :: println);

Get stream from directory

public static void main(String[] args) throws IOException{
        Path path = Paths.get("c://");
        Stream<Path> stream = Files.list(path);
        stream.forEach(p -> System.out.println(p.getFileName()));

Recommended Posts

[JAVA] Stream type
Java type conversion
[Java] Enumeration type
Try Java 8 Stream
Java Optional type
Java Stream API
Java double type
Studying Java 8 (Stream)
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
[Java] Stream Collectors notes
[Java] Stream API-Stream generation
[Java] Stream API / map
[Java] Data type ①-Basic type
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
Java8 Stream API practice
Java8 Stream reduction operation
[Java] About enum type
Java8 Stream Rough Summary
[Java] Date type conversion
[Java] List type / Array type conversion
Java learning memo (data type)
[Java11] Stream Summary -Advantages of Stream-
Try functional type in Java! ①
Java Stream API cheat sheet
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
I tried Java Lambda input / output type ~ Stream version ~
Java Stream API in 5 minutes
Java8 stream, lambda expression summary
Java study # 7 (branch syntax type)
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
Java Stream cannot be reused.
[Java] Introduction to Stream API
Use Redis Stream in Java
Java Primer Series (Type Conversion)
[Java11] Stream Usage Summary -Basics-
Java application for beginners: stream
[Java] Stream API intermediate operation
[Java8] Sort int type array in descending order using stream
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Stream (filter, map, forEach, reduce)
[java8] To understand the Stream API
About Lambda, Stream, LocalDate of Java8
[Introduction to Java] About Stream API
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
[Java] Element existence check with Stream
java (use class type for field)
I tried using Java8 Stream API
Basic processing flow of java Stream
[Java] Correct comparison of String type
Java 8 ~ Stream API ~ to start now
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map