Java Stream How to - Check if any String in the list starts with a letter








Question

We would like to know how to check if any String in the list starts with a letter.

Answer

import java.util.ArrayList;
import java.util.List;
//  ww w.j  av a2s. c  o  m
public class Main {

  public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    boolean anyStartsWithA = stringCollection.stream().anyMatch(
        (s) -> s.startsWith("a"));

    System.out.println(anyStartsWithA); // true
  }

}

The code above generates the following result.