Back
Close

Search data from a Stream

Harinath
2,215 views

Search for the data by using the methods of Stream classes... findFirst findAny anyMatch allMatch noneMatch

Methods ending with "Match" returns boolean value. Methods starting with "find" returns return Optional (we discuss Optional in further playgrounds)

// {...}
//returns false
boolean anyMatch
= IntStream.of(-6, -7, -5, -2, -8, -1, -9).anyMatch(value -> value > 0);
System.out.println("anyMatch(value -> value > 0): " + anyMatch);
//returns false
boolean allMatch
= IntStream.of(-6, -7, -5, -2, -8, -1, -9).allMatch(value -> value > 0);
System.out.println("allMatch(value -> value > 0): " + allMatch);
//returns true
boolean noneMatch
= IntStream.of(-6, -7, -5, -2, -8, -1, -9).noneMatch(value -> value > 0);
System.out.println("noneMatch(value -> value > 0): " + noneMatch);
//returns 5
OptionalInt optValue = IntStream.of(-6, -7, 5, -2, -8, 1, 9)
.filter(value -> value > 0)
.findFirst();
System.out.println("First matching value > 0 is " + optValue.getAsInt());
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants