1. 기본적인 for문을 활용한 출력 방법
import java.util.List;
public class FP01Structured {
public static void main(String[] args) {
pringAllNumberInListStructured(List.of(12, 9, 13,4,6,2,4,12,15));
}
private static void pringAllNumberInListStructured(List<Integer> numbers) {
// How to loop the numbers?
for(int number : numbers) {
System.out.println(number);
}
}
}
2. 함수형 자바를 사용한 출력 방법
import java.util.List;
public class FP01Functional {
public static void main(String[] args) {
pringAllNumberInLisFunctional(List.of(12,9,13,4,6,2,4,12,15));
}
private static void print(int number) {
System.out.println(number);
}
private static void pringAllNumberInLisFunctional(List<Integer> numbers) {
// [12,9,13,4,6,2,4,12,15] -> 리스트를 스트림으로 변경해야함
// 12
// 9
// 13
// 4
// What to do?
numbers.stream().forEach(FP01Functional::print);
// 메서드 참조호출로 클래스명::메서드명 형식으로 사용 가능
// How to loop the numbers?
// for(int number : numbers) {
// System.out.println(number);
// }
}
}
3. static 메서드 호출이 아닌 출력을 바로 호출하는 방식
import java.util.List;
public class FP01Functional2 {
public static void main(String[] args) {
pringAllNumberInLisFunctional(List.of(12,9,13,4,6,2,4,12,15));
}
private static void pringAllNumberInLisFunctional(List<Integer> numbers) {
// What to do?
numbers.stream().forEach(System.out::println);
// 메서드 참조호출로 클래스명::메서드명 형식으로 사용 가능
// How to loop the numbers?
// for(int number : numbers) {
// System.out.println(number);
// }
}
}
'JAVA' 카테고리의 다른 글
3. 함수형 문법을 사용한 예제 (0) | 2024.10.01 |
---|---|
2. 함수형 문법 사용 filter() (0) | 2024.10.01 |
스트림 3 (0) | 2023.03.03 |
스트림2 (0) | 2023.02.24 |
자바 강의 7 (0) | 2023.01.27 |