Filter method of the Stream class

  • Finding 10 people whos age is less than 18
  @Test
  public void declarativeApproachUsingStreams() throws Exception {
    List<Person> people = MockData.getPeople();
    
    people.stream()
      .filter(p -> p.getAge() <= 18)
      .limit(10)
      .collect(Collectors.toList())
      .forEach(System.out::println);
  }
public class Main {
	public static void main(String[] args) throws IOException {
		   
		  List<String> namesList = Arrays.asList("Tyson","Justin","Martin");
		  
		  namesList = namesList.stream().filter(x -> x.startsWith("T")).collect(Collectors.toList());
		  
		  namesList.stream().forEach(System.out::println);
	}
}

Leave a Comment