假设以下是我们的字符串列表-
List list = Arrays.asList("Jonny", "David", "Tony", "Jacob", "Smith", "Bravo", "Gayle", "John");
现在以首字母过滤字符串值-
Stream stream = list.stream().filter(name -> name.startsWith("J"));
以下是一个以开头字母过滤字符串值的示例-
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())); } }
输出结果
List with elements... Jonny David Tony Jacob Smith Bravo Gayle John Getting strings beginning with letter J = [Jonny, Jacob, John]