Java Program to check if none of the string in the list matches the condition

Java 8Object Oriented ProgrammingProgramming

First, create a List with String elements:

List<String> myList = new ArrayList<>();
myList.add("pqr");
myList.add("stu");
myList.add("vwx");
myList.add("yza");
myList.add("bcd");
myList.add("efg");
myList.add("vwxy");

Use the noneMatch() method to check if none of the above string in the myList begins with a specific letter:

myList.stream().noneMatch((a) -> a.startsWith("f"));

TRUE is returned if none of the string begins with the specific letter, else FALSE.

The following is an example to check if none of the string in the list matches the condition:

Example

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(final String[] args) {
      List<String> myList = new ArrayList<>();
      myList.add("pqr");
      myList.add("stu");
      myList.add("vwx");
      myList.add("yza");
      myList.add("bcd");
      myList.add("efg");
      myList.add("vwxy");
      boolean res = myList.stream().noneMatch((a) -> a.startsWith("f"));
      System.out.println("No match for the starting letter as f? = "+res);
   }
}

Output

No match for the starting letter as f? = true
raja
Published on 02-May-2019 08:12:54
Advertisements