본문 바로가기
자바

[JAVA] Stream의 Match 사용법

Stream Match -  allMatch, anyMatch, noneMatch 사용법

 

allMatch - 모든 요소가 true인지 체크

 - 하나라도 false면 false 반환

 - 모든 요소가 true면 true 반환

anyMatch - 하나라도 true인지 체크

 - 하나라도 true면 true반환

 - 모든 요소가 false면 false반환

noneMatch - 모든 요소가 false인지 체크

 - 하나라도 true면 false 반환

 - 모든 요소가 false면 true 반환

 

int[] intArr = {2, 4, 6, 8 , 10, 12};

log.info("all~");
boolean result = Arrays.stream(intArr)
        .allMatch(a -> {
            log.info("a : {}, res : {}", a, a%2 == 0);
            return a%2 == 0;
        });
System.out.println("2의 배수? " + result);

log.info("any~");
result = Arrays.stream(intArr)
        .anyMatch(a -> {
            log.info("a : {}, res : {}", a, a%3 == 0);
            return a%3 == 0;
        });
System.out.println("3의 배수가 하나라도 있나? " + result);

log.info("none~");
result = Arrays.stream(intArr)
        .noneMatch(a -> {
            log.info("a : {}, res : {}", a, a%3 == 0);
            return a%3 == 0;
        });
System.out.println("3의 배수가 없나? " + result);

 

참조 글 : https://cornswrold.tistory.com/300

'자바' 카테고리의 다른 글

[Tomcat] zip file is empty  (0) 2022.12.09
[JAVA] JAVA 코드로 Wake On Lan 사용하기  (0) 2022.08.16
[JPA] Change Column Name 유의  (0) 2022.05.23
RFID 리더기로 값 읽기 - 프롤로그  (0) 2021.07.14