Created
April 10, 2017 10:05
-
-
Save kodaitakahashi/1db161d4a8b6b08fa0d971f571dda107 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.Supplier; | |
import java.util.stream.DoubleStream; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class StreamEx { | |
/* | |
* ストリームAPI | |
* | |
* ・複数の処理の入出力をつなぐための仕組み -> パイプライン処理 | |
* | |
* パイプライン処理に必要なもの | |
* | |
* ・問い合わせにの対象となるデータソース -> コレクション、 配列、 I/Oリソースなど | |
* | |
* ・中間処理 -> ストリーム、パイプラインを形成 | |
* | |
* ・終端処理 -> ストリーム、パイプラインを実行して結果を生成 (終端処理がされるまで中間処理は動かない) | |
*/ | |
static final List<String> STRINGS = new ArrayList<String>() { | |
{ | |
add("a"); | |
add("b"); | |
add("c"); | |
} | |
}; | |
public static void main(String[] args) { | |
// default Stream<E> stream(); | |
// Collectionインターフェースで提供 | |
// ListのスーパーインターフェースとしてCollection<E> Iterable<E>がある | |
Stream<String> stream1 = new ArrayList<String>() { | |
{ | |
add("a"); | |
add("b"); | |
} | |
}.stream(); | |
// static Stream<T> stream(T[] array) | |
// Arraysクラスで提供 | |
Stream<String> stream2 = Arrays.asList("a", "b", "c", "d").stream(); | |
// static IntStream stream(int[] array) | |
// Arraysクラスで提供 | |
IntStream stream3 = Arrays.stream(new int[] { 1, 2, 3, 4 }); | |
// static <T> Stream<T> of(T t) | |
// Streamインターフェースが提供 | |
Stream<String> of = Stream.of("a"); | |
// static <T> Stream<T> of(T[] tArray) | |
// 同上 | |
Stream<List<String>> of2 = Stream.of(STRINGS); | |
// static <T> Stream<T> empty() | |
// Streamインターフェースが提供 | |
// 空のStreamオブジェクトを返す | |
Stream<String> empty = Stream.empty(); | |
// static <T> Stream<T> generate (Supplier<T> s) | |
// Streamインターフェスが提供 | |
// 引数として渡したSuppilerによって生成される要素に対する順序付けされていないStreamオブジェクトを返す | |
Supplier<String> supplier = String::new; | |
Stream<String> generate = Stream.generate(supplier); | |
// static <T> Stream<T> iterate(T seed, UnaryOperator<T>) | |
// Streamインターフェスが提供 | |
// 順序付けされた無限順次Streamを返す | |
// static DoubleStream of(double) | |
// static DoubleStream of(double[] array) | |
// DoubleStreamインターフェースが提供 | |
DoubleStream of3 = DoubleStream.of(10.1); | |
DoubleStream of4 = DoubleStream.of(new double[] { 10, 20, 30 }); | |
// static IntStream range(int startinclusive, int endExclusive) | |
// IntStreamインターフェースが提供 | |
// startinclusiveを含くむ数字からendExclusiceを含まない範囲の順次付けされた順次IntStreamを返す | |
IntStream.range(0, 10).forEach(num -> System.out.print(num + " ")); | |
System.out.println(); | |
// static IntStream rangeClosed(int startinclusice, int endinclusive) | |
// IntStreamインターフェースが提供 | |
// startinclusiveを含くむ数字からendinclusiveを含む範囲の順次付けされた順次IntStreamを返す | |
IntStream.rangeClosed(0, 10).forEach(num -> System.out.print(num + " ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment