Filter String Value by Starting Letter



Let’s say the following is our String List −

List list = Arrays.asList("Jonny", "David", "Tony", "Jacob", "Smith", "Bravo", "Gayle", "John");

Now filter the string value with starting letter −

Stream stream = list.stream().filter(name -> name.startsWith("J"));

The following is an example to filter string value with starting letter −

Example

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      List list = Arrays.asList("Jonny", "David", "Tony", "Jacob", "Smith",
         "Bravo", "Gayle", "John");
      System.out.println("List with elements...");
      for (String res : list) {
         System.out.print(res+" ");
      }
      Stream stream = list.stream().filter(name -> name.startsWith("J"));
      System.out.println("\nGetting strings beginning with letter J =
       "+stream.collect(Collectors.toList()));
   }
}

Output

List with elements...
Jonny David Tony Jacob Smith Bravo Gayle John
Getting strings beginning with letter J = [Jonny, Jacob, John]
Updated on: 2019-07-30T22:30:26+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements