Created
April 10, 2017 10:06
-
-
Save kodaitakahashi/47569b3c5478529ffb6e023fbf1c7670 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.List; | |
import java.util.function.IntPredicate; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class StreamEx1 { | |
// 終端処理 | |
static final IntPredicate intPredicate = num -> num > 100; | |
public static void main(String[] args) { | |
// boolean allMatch(<Predicate<? super T>> predicate) | |
// すべての要素が指定された条件に一致しているかどうかを調べる | |
// すべての要素が条件とマッチした場合、Streamが空だった場合 true | |
// それ以外の場合 false | |
System.out.println(IntStream.rangeClosed(50, 150).allMatch(intPredicate)); | |
System.out.println(IntStream.rangeClosed(101, 200).allMatch(intPredicate)); | |
// boolean anyMatch(<Predicate<? super T>> predicate) | |
// いずれかの要素の要素が指定された条件に一致しているかどうかを調べる | |
// いずれかの要素が条件とマッチした場合、Streamが空だった場合 true | |
// それ以外の場合 false | |
System.out.println(IntStream.rangeClosed(0, 101).anyMatch(intPredicate)); | |
// boolean noneMatch(<Predicate<? super T>> predicate) | |
// すべての要素の要素が指定された条件に一致していないかどうかを調べる | |
// すべての要素が条件とマッチしていない場合、Streamが空だった場合 true | |
// それ以外の場合 false | |
System.out.println(IntStream.rangeClosed(0, 100).noneMatch(intPredicate)); | |
System.out.println(IntStream.rangeClosed(100, 101).noneMatch(intPredicate)); | |
// <R, A> R collect (Collector <? super T, A, R> collect) | |
// 要素に対する可変リダイレクションを操作を実行する | |
Stream<String> of = Stream.of("a", "b", " c"); | |
List<String> collect = of.collect(Collectors.toList()); | |
Stream<String> of2 = Stream.of("a", "b", "c", "d", "c", "hoge"); | |
String collect2 = of2.collect(Collectors.joining("|")); | |
System.out.println(collect2); | |
// long Count() | |
// 要素数を返す | |
Stream<Integer> of3 = Stream.of(1, 2, 3, 4, 5); | |
long count = of3.count(); | |
System.out.println(count); | |
// reduce | |
int reduce = IntStream.rangeClosed(1, 10).reduce(0, Integer::sum); | |
System.out.println(reduce); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment