filter

jiiiiiiiArchive.·2025년 2월 3일

🤯지식주머니🤯

목록 보기
71/98

filter()

Stream의 중간 연산(intermediate operation) 중 하나로,
특정 조건을 만족하는 요소만 걸러서 새로운 스트림을 생성한다.

✅사용 예제

List<String> names = List.of("Alice", "Bob", "Charlie", "David");

// 이름 길이가 4보다 큰 요소만 필터링
List<String> filteredNames = names.stream()
        .filter(name -> name.length() > 4)
        .collect(Collectors.toList());

System.out.println(filteredNames); // [Alice, Charlie, David]
  • 동작 과정
  1. names.stream() : 리스트를 스트림으로 변환
  2. .filter(name -> name.length() > 4) : 길이가 4보다 큰 문자열만 남김
  3. .collect(Collectors.toList()) : 필터링된 결과를 리스트로 변환
profile
이것저것 다 적는 기록장📚

0개의 댓글