How to filter and collect a list of strings that start with a specific letter using Java 8 Stream
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FilterBySpecificCharcter {
public static void main(String[] args) {
//How to filter and collect a list of strings that start with a specific letter using Java 8 Stream
String[] str = { "Rajesh", "Rahul", "Verma", "Patel" };
List<String> list = Stream.of(str).filter(f -> f.startsWith("R")).collect(Collectors.toList());
System.out.print(list);
}
}
OutPut
[Rajesh, Rahul]