How can I filter string value with starting letter?

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 09-May-2019 08:56:30
Advertisements