[Java] Stream API-Stream generation

What is the Steam API?

Stream API basics

import java.util.ArrayList;
import java.util.Arrays;

public class StreamBasic {

//Data source preparation
  public static void main(String[] args) {
    var list = new ArrayList<String>(Arrays.asList("ant", "elephant", "hamster"));

/*Processing by stream
Stream generation from a pre-prepared list
ArrayList<String>Because it is based on
stream method is also Stream<String>Returns an object
*/
    list.
      stream().
      /*Intermediate processing
Extract 3 or more characters with the filter method and convert to uppercase with map
The return value is Stream<T>
      */
      filter(s -> s.length() > 3).
      map(String::toUpperCase).
      /*Termination
System the value given by the forEach method.out
      */
      forEach(System.out::println);//ELEPHANT HAMSTER
  }
}

Stream generation

** Stream generation from collections and arrays **

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class StreamBasic2 {

//list
  public static void main(String[] args) {
    var list = new ArrayList<String>(Arrays.asList("ant", "elephant", "hamster"));
    list.stream().forEach(System.out::println);
    // list.parallelStream().forEach(System.out::println);
    System.out.println("---------------");

//Array
    var data = new String[] { "rose", "Morning glory", "Tulips" };
    Arrays.stream(data).forEach(System.out::println);
    System.out.println("---------------");

//map
    var map = new HashMap<String, String>(
        Map.of("orange", "Mandarin orange", "apple", "Apple", "strawberry", "Strawberry"));
    map.entrySet().stream().forEach(System.out::println);
  }
}

** Stream generation from arguments / lambda expressions **

** of method **

import java.util.stream.Stream;

public class StreamOf {
  public static void main(String[] args) {
    var stream = Stream.of("first", "second", "third");
    stream.forEach(System.out::println); //first, second, third
  }
}

** generate method **

import java.util.Random;
import java.util.stream.Stream;

public class Main {
public static void main(String[] args) throws Exception {

    var stream = Stream.generate(() -> {
      var r = new Random();
      return r.nextInt(100);
    });
    stream.limit(10).forEach(System.out::println);
    //46 24 42 80 61 67 69 22 85 1
  }
}

** iterate method **

import java.util.stream.Stream;
public class Main {
  public static void main(String[] args) {
    var stream = Stream.iterate(1, (num) -> {
      return num * 2;
    });
    stream.limit(10).forEach(System.out::println);
    //4 8 16 32 64 128 256 512
    }
}

** builder method **

import java.util.stream.Stream;
public class Main {
  public static void main(String[] args) {
    var builder = Stream.builder()
        .add("Munchkin")
        .add("Siamese")
        .add("Persian");
      builder.build().forEach(System.out::println); 
      //Munchkin
      //Siamese
      //Persian
  }
}

** concat method **

import java.util.stream.Stream;
public class Main {
  public static void main(String[] args) {

    var stream1 = Stream.of("Munchkin", "Siamese", "Persian");
    var stream2 = Stream.of("Corgi", "Poodle", "Shiba");
    Stream.concat(stream1, stream2).forEach(System.out::println);
    // Munchkin Siamese Persian 
    // Corgi Poodle Shiba
  }
}

** Generate basic stream **

//Upper limit A h Not included in the stream
import java.util.stream.IntStream;
public class Main {
  public static void main(String[] args) {
    IntStream.range(10, 20)
      .forEach(System.out::println); //10 11 12 13 14 15 16 17 18 19
  }
}
import java.util.stream.IntStream;
public class Main {
  public static void main(String[] args) {
    IntStream.rangeClosed(10, 20)
     .forEach(System.out::println); //10 11 12 13 14 15 16 17 18 19 20
  }
}

Recommended Posts

[Java] Stream API-Stream generation
Try Java 8 Stream
Java Stream API
Stream generation process
Java permutation generation
Studying Java 8 (Stream)
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
[Java] Stream Collectors notes
[Java] Stream API / map
Java8 Stream API practice
Java8 Stream reduction operation
[Java] Password generation (Pasay)
Java8 Stream Rough Summary
[Java11] Stream Summary -Advantages of Stream-
Java Stream API cheat sheet
Java Stream API in 5 minutes
Java8 stream, lambda expression summary
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Random number generation method (Random)
Java Stream cannot be reused.
[Java] Introduction to Stream API
Use Redis Stream in Java
[Java Silver] Array generation method
[Java11] Stream Usage Summary -Basics-
Java application for beginners: stream
[Java] Stream API intermediate operation
[Java] New Thread generation method (1)
Recent Java API specification generation
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Stream (filter, map, forEach, reduce)
[java8] To understand the Stream API
[Introduction to Java] About Stream API
[Java] Element existence check with Stream
Java
I tried using Java8 Stream API
Basic processing flow of java Stream
Java
Java 8 ~ Stream API ~ to start now
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map
Do you use Stream in Java?
Data processing using stream API from Java 8
Try using the Stream API in Java
Nowadays Java lambda expressions and Stream API
Sort by multiple conditions using Java Stream
Try various Java Stream API methods (now)
Introduction of New Generation Java Programming Guide (Java 10)
Introduction of New Generation Java Programming Guide (Java 11)
Introduction of New Generation Java Programming Guide (Java 12)