JavaのStreamのサンプルです。
目次
- ストリーム (Stream)
- 要素の数を数える(count)
- 要素の重複をなくす(distinct)
- 要素を並び替える(sorted)
- 要素の条件が全て一致するか(allMatch)
- 要素の条件が1つでも一致するか(anyMatch)
- 要素の条件が一致するものは1つもないか(noneMatch)
- 並列に処理する(parallelStream/parallel)
ストリーム (Stream)
- コレクション・配列やファイル・通信などに対して使用します。
- 配列や指定した値等からストリームを生成し、そのストリームに対して処理します。
- 並列処理を行うことができます。
- 外部のローカル変数に対してアクセスできるのはfinal変数のみです。(再代入不可の変数)
→並列で処理ができるので値が変わらない変数のみアクセス可能です。
コレクションでストリームを使用する
- コレクションでStreamのインスタンスを生成するにはCollectionインターフェースのstreamメソッドを使用します。
- ArrayListなどのコレクションのクラスは、Collectionインターフェースを実装しているのでstreamメソッドを使用できます。
- 以下は、OracleのJava8のCollectionクラスのstreamメソッドのリンクです。
https://docs.oracle.com/javase/jp/8/docs/api/java/util/Collection.html#stream--
配列でストリームを使用する
- 配列でStreamのインスタンスを生成するにはArraysクラスのstreamメソッドを使用します。
- Arraysクラスは、配列を操作するためのクラスです。
- 以下は、OracleのJava8のArraysクラスのstreamメソッドのリンクです。
https://docs.oracle.com/javase/jp/8/docs/api/java/util/Arrays.html#stream-T:A-
要素の数を数える(count)
コレクションの要素の数を数えるサンプルです。
package test1;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Test1 {
public static void main(String[] args) {
List<String> colors = new ArrayList<>();
colors.add("red");
colors.add("yellow");
colors.add("blue");
long a = colors.stream()
.count();
System.out.println(a); //3
long b = colors.stream().count();
System.out.println(b); //3
Stream<String> c = colors.stream();
System.out.println(c.count()); //3
}
}
14,15行目は、ストリームを生成してcountメソッドを実行し、戻り値を変数にセットしています。
18行目は、ストリームを生成しcountメソッドを実行して戻り値を変数にセットしています。
21行目は、ストリームを生成し22行目でcountメソッドを実行しています。
要素の重複をなくす(distinct)
コレクションの要素の重複をなくすサンプルです。
package test1;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
List<String> colors = new ArrayList<>();
colors.add("red");
colors.add("blue");
colors.add("blue");
colors.stream()
.distinct()
.forEach(b -> System.out.println(b)); //red blue
}
}
13行目は、ストリームを生成しています。
14行目は、distinctメソッドを実行しています。
15行目は、forEachメソッドを実行しています。
要素を並び替える(sorted)
コレクションの要素を並び替えるサンプルです。
package test1;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
List<String> num1 = new ArrayList<>();
num1.add("aa");
num1.add("zz");
num1.add("cc");
num1.stream()
.sorted()
.forEach(b -> System.out.println(b)); //aa cc zz
num1.stream()
.sorted(Comparator.reverseOrder())
.forEach(b -> System.out.println(b)); //zz cc aa
}
}
14行目は、sortedメソッドに引数がありません。自然順序の昇順になります。
18行目は、sortedメソッドの引数にComparatorインターフェースのstaticメソッドのreverseOrderメソッドを指定しています。降順になります。自然順序の逆になります。
以下は、Java8のComparatorインターフェースのreverseOrderメソッドのリンクです。
https://docs.oracle.com/javase/jp/8/docs/api/java/util/Comparator.html#reverseOrder--
要素の条件が全て一致するか(allMatch)
コレクションの要素の条件が全て一致するか確認するサンプルです。
package test1;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
List<Integer> num1 = new ArrayList<>();
num1.add(5);
num1.add(8);
num1.add(9);
//すべての要素は7より大きいか
boolean a = num1.stream()
.allMatch(b -> b > 7);
System.out.println(a); //false
//すべての要素は3より大きいか
boolean c = num1.stream()
.allMatch(d -> d > 3);
System.out.println(c); //true
}
}
allMatchメソッドは全て一致すればtrueを返します。
14,15行目は、すべての要素は7より大きいか確認しています。→false
20,21行目は、すべての要素は3より大きいか確認しています。→true
要素の条件が1つでも一致するか(anyMatch)
コレクションの要素の条件が1つでも一致するか確認するサンプルです。
package test1;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
List<Integer> num1 = new ArrayList<>();
num1.add(5);
num1.add(8);
num1.add(9);
//10より大きい要素はあるか
boolean a = num1.stream()
.anyMatch(b -> b > 10);
System.out.println(a); //false
//9より大きい要素はあるか
boolean c = num1.stream()
.anyMatch(d -> d > 3);
System.out.println(c); //true
}
}
anyMatchメソッドは1つでも一致すればtrueを返します。
14,15行目は、10より大きい要素はあるか確認しています。→false
20,21行目は、9より大きい要素はあるか確認しています。→true
要素の条件が一致するものは1つもないか(noneMatch)
コレクションの要素の条件が一致するものは1つもないか確認するサンプルです。
package test1;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
List<Integer> num1 = new ArrayList<>();
num1.add(5);
num1.add(8);
num1.add(9);
//7より大きい要素はひとつもないか
boolean a = num1.stream()
.noneMatch(b -> b > 7);
System.out.println(a); //false
//10より大きい要素はひとつもないか
boolean c = num1.stream()
.noneMatch(d -> d > 10);
System.out.println(c); //true
}
}
noneMatchメソッドは一致が1つもない場合にtrueを返します。
14,15行目は、7より大きい要素はひとつもないか確認しています。→false
20,21行目は、10より大きい要素はひとつもないか確認しています。→true
並列に処理する(parallelStream/parallel)
コレクションを並列に処理するサンプルです。
parallelStreamメソッドまたはparallelメソッドを使用します。
package test1;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
List<String> colors = new ArrayList<>();
colors.add("red");
colors.add("yellow");
colors.add("blue");
colors.add("green");
colors.parallelStream()
.forEach(b -> System.out.println(b));
//yellow red blue green
colors.stream()
.parallel()
.forEach(b -> System.out.println(b));
//yellow red blue green
}
}
要素が並列に処理されるので、返される値の順番は並び順ではありません。
14行目は、parallelStreamメソッドです。
19行目は、parallelメソッドです。
関連の記事
Java ラムダ式のサンプル
Java ラムダ式で関数型インターフェースを使用するサンプル
Java ストリームでプリミティブ型の数値を使用するサンプル