Program/ Example -
//short-circuiting stateful intermediate operation
//findFirst() method - returns first element in the stream
OptionalInt optionalInt = IntStream.of(1, 2, 3, 4, 5).findFirst();
System.out.println("optionalInt.getAsInt() = "+optionalInt.getAsInt());
//findAny() method - returns any element in the stream
OptionalInt optionalInt1 = IntStream.of(1, 2, 3, 4, 5).findAny();
System.out.println("optionalInt2.getAsInt() = "+optionalInt1.getAsInt());
// Find whether any record greater than 2
boolean anyRecordGreaterThan = IntStream.of(1, 2, 3, 4, 5).anyMatch((n) -> n > 2 );
System.out.println("Any Record greater than 2 = " + anyRecordGreaterThan); //true
// Find whether ALL records greater than 2
boolean allRecordGreaterThan = IntStream.of(1, 2, 3, 4, 5).allMatch((n) -> n > 2 );
System.out.println("All Record greater than 2 = " + allRecordGreaterThan); //false
// Find whether NONE of the record greater than 6
boolean noneRecordGreaterThan = IntStream.of(1, 2, 3, 4, 5).noneMatch((n) -> n > 6 );
System.out.println("None Record greater than 6 = " + noneRecordGreaterThan); //true
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.