Java filter(), Optional example - find the LCM

Byul·2023년 6월 13일

Java

목록 보기
5/10

Find the LCM (least common multiple) of 8 and 7 from numbers 1 to 100;

my approach:

int num = IntStream.range(1, 101).boxed().filter(n -> n % 5 == 0 && n % 7 == 0).findFirst().get();
System.out.println(new Integer(num) != null ? "found " + num : "none found");

chatGPT:

OptionalInt num =  IntStream.range(1, 101).filter(n -> n % 5 == 0 && n % 7 == 0).findFirst();
System.out.println(num.isPresent() ? "found " + num.getAsInt() : "none found");
profile
junior backend developer

0개의 댓글