I have a collection of items as under
List<String> lstRollNumber = new ArrayList<String>();
lstRollNumber.add("1");
lstRollNumber.add("2");
lstRollNumber.add("3");
lstRollNumber.add("4");
Now I want to search a particular RollNumber in that collection. Say
String rollNumberToSearch = "3";
I can easily do it by looping through the collection and checking for every items and if there is any match, i can break through the loop and return a true from the function.
But I want to use the Lambda expression for doing this.
In C# we use(among other options),
var flag = lstRollNumber.Exists(x => x == rollNumberToSearch);
How to do the same in Java 1.8 ?
I tried with
String rollNumberToSearch = "3";
Stream<String> filterRecs = lstRollNumbers.stream().filter(rn -> rn.equals(rollNumberToSearch));
But I know it is wrong? Please guide.