문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:book_source:ch15 [2022/01/04 11:30] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch15 [2023/07/13 17:29] (현재) clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch15에서 프로그램:java:ㄴhomework:book_source:ch15(으)로 이동되었습니다 |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
+ | |||
+ | ==== 람다식 개념 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | public class LambdaFunctionEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | InterfaceEx ie = (int x, int y) -> x+y; | ||
+ | |||
+ | System.out.println(ie.sum(1, | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | interface InterfaceEx { | ||
+ | public int sum(int x, int y); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== 함수적 인터페이스 ==== | ||
+ | |||
+ | === LambdaEx === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | public class LambdaEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | LambdaInterface li = () -> { | ||
+ | String str = " | ||
+ | System.out.println(str); | ||
+ | }; | ||
+ | |||
+ | li.print(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | interface LambdaInterface { | ||
+ | void print(); | ||
+ | //void print2(); // 오류발생 | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === LambdaEx2 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | public class LambdaEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.println(" | ||
+ | Runnable run = () -> { | ||
+ | for (int i=1; i<=10; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }; | ||
+ | Runnable run2 = () -> { | ||
+ | for (int i=1; i<=10; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | Thread t = new Thread(run); | ||
+ | Thread t2 = new Thread(run2); | ||
+ | t.start(); | ||
+ | t2.start(); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | === LambdaEx3 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | public class LambdaEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | LambdaInterface3 li3 = (String name) -> { | ||
+ | System.out.println(" | ||
+ | }; | ||
+ | |||
+ | li3.print(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | @FunctionalInterface | ||
+ | interface LambdaInterface3 { | ||
+ | void print(String name); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === LambdaEx4 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | public class LambdaEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | LambdaInterface4 f4 = (x,y) -> { | ||
+ | return x * y; | ||
+ | }; | ||
+ | System.out.println(" | ||
+ | |||
+ | f4 = (x, y) -> x + y; | ||
+ | System.out.println(" | ||
+ | |||
+ | f4 = (x, y) -> { return x/y; }; | ||
+ | System.out.println(" | ||
+ | |||
+ | f4 = (x, y) -> x%y; | ||
+ | System.out.println(" | ||
+ | |||
+ | f4 = (x,y) -> sum(x, y); | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | static int sum(int x, int y) { | ||
+ | return x+y; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | @FunctionalInterface | ||
+ | interface LambdaInterface4 { | ||
+ | int cal(int x, int y); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === LambdaEx5 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | public class LambdaEx5 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Outer o = new Outer(); | ||
+ | o.method(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | @FunctionalInterface | ||
+ | interface LambdaInterface5 { | ||
+ | void method(); | ||
+ | } | ||
+ | |||
+ | class Outer { | ||
+ | public int iv = 10; | ||
+ | void method() { | ||
+ | final int iv = 40; | ||
+ | LambdaInterface5 f5 = () -> { | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | }; | ||
+ | f5.method(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === LambdaEx6 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | import java.util.function.BiConsumer; | ||
+ | import java.util.function.Consumer; | ||
+ | import java.util.function.DoubleConsumer; | ||
+ | import java.util.function.ObjIntConsumer; | ||
+ | |||
+ | public class LambdaEx6 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Consumer< | ||
+ | c1.accept(" | ||
+ | |||
+ | BiConsumer< | ||
+ | System.out.println(" | ||
+ | }; | ||
+ | c2.accept(" | ||
+ | |||
+ | DoubleConsumer c3 = (score) -> System.out.println(" | ||
+ | c3.accept(95.5); | ||
+ | |||
+ | ObjIntConsumer< | ||
+ | System.out.println(" | ||
+ | }; | ||
+ | c4.accept(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | import java.util.function.DoubleSupplier; | ||
+ | import java.util.function.IntSupplier; | ||
+ | import java.util.function.Supplier; | ||
+ | |||
+ | public class LambdaEx7 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Supplier< | ||
+ | return " | ||
+ | }; | ||
+ | System.out.println(s1.get()); | ||
+ | |||
+ | s1 = () -> " | ||
+ | System.out.println(s1.get()); | ||
+ | |||
+ | IntSupplier s2 = () -> { | ||
+ | int num = (int)(Math.random() * 6) + 1; | ||
+ | return num; | ||
+ | }; | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | DoubleSupplier s3 = () -> Math.PI; | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | === LambdaEx8 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | import java.util.function.Function; | ||
+ | import java.util.function.ToDoubleFunction; | ||
+ | import java.util.function.ToIntFunction; | ||
+ | |||
+ | public class LambdaEx8 { | ||
+ | |||
+ | static Student[] list = { | ||
+ | new Student(" | ||
+ | new Student(" | ||
+ | }; | ||
+ | |||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.print(" | ||
+ | printString(t -> t.getName()); | ||
+ | System.out.print(" | ||
+ | printString(t -> t.getMajor()); | ||
+ | System.out.print(" | ||
+ | printInt(t -> t.getEng()); | ||
+ | System.out.print(" | ||
+ | printInt(t -> t.getMath()); | ||
+ | System.out.print(" | ||
+ | printTot(t -> t.getEng()); | ||
+ | System.out.print(" | ||
+ | printTot(t -> t.getMath()); | ||
+ | System.out.print(" | ||
+ | printAvg(t -> t.getEng()); | ||
+ | System.out.print(" | ||
+ | printAvg(t -> t.getMath()); | ||
+ | |||
+ | } | ||
+ | |||
+ | static void printAvg(ToDoubleFunction< | ||
+ | double sum = 0; | ||
+ | for (Student s : list) { | ||
+ | sum += f.applyAsDouble(s); | ||
+ | } | ||
+ | System.out.println(sum / list.length); | ||
+ | } | ||
+ | |||
+ | static void printTot(ToIntFunction< | ||
+ | int sum = 0; | ||
+ | for (Student s : list) { | ||
+ | sum += f.applyAsInt(s); | ||
+ | } | ||
+ | System.out.println(sum); | ||
+ | } | ||
+ | |||
+ | static void printInt(Function< | ||
+ | for (Student s : list) { | ||
+ | System.out.print(f.apply(s) + " "); | ||
+ | } | ||
+ | System.out.println(); | ||
+ | } | ||
+ | |||
+ | static void printString(Function< | ||
+ | for (Student s : list) { | ||
+ | System.out.print(f.apply(s) + " "); | ||
+ | } | ||
+ | System.out.println(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class Student { | ||
+ | private String name; | ||
+ | private int eng; | ||
+ | private int math; | ||
+ | private String major; | ||
+ | |||
+ | public Student(String name, int eng, int math, String major) { | ||
+ | this.name = name; | ||
+ | this.eng = eng; | ||
+ | this.math = math; | ||
+ | this.major = major; | ||
+ | } | ||
+ | public String getName() { | ||
+ | return name; | ||
+ | } | ||
+ | public int getEng() { | ||
+ | return eng; | ||
+ | } | ||
+ | public int getMath() { | ||
+ | return math; | ||
+ | } | ||
+ | public String getMajor() { | ||
+ | return major; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === LambdaEx9 === | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | import java.util.function.DoubleBinaryOperator; | ||
+ | import java.util.function.IntBinaryOperator; | ||
+ | |||
+ | public class LambdaEx9 { | ||
+ | |||
+ | // 앞 예제에서 작성한 Student 클래스의 배열 | ||
+ | static Student[] list = { | ||
+ | new Student(" | ||
+ | new Student(" | ||
+ | new Student(" | ||
+ | }; | ||
+ | |||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.print(" | ||
+ | int max = maxOrMinMath( (a,b) -> { | ||
+ | if(a> | ||
+ | else return b; | ||
+ | }); | ||
+ | System.out.println(max); | ||
+ | |||
+ | System.out.print(" | ||
+ | System.out.println(maxOrMinMath((a, | ||
+ | |||
+ | System.out.print(" | ||
+ | System.out.println(maxOrMinAvg((a, | ||
+ | |||
+ | System.out.print(" | ||
+ | System.out.println(maxOrMinAvg((a, | ||
+ | |||
+ | } | ||
+ | |||
+ | // 두 개의 int 값을 연산하여 int값을 리턴 | ||
+ | private static int maxOrMinMath(IntBinaryOperator op) { | ||
+ | int result = list[0].getMath(); | ||
+ | for(Student s : list) { | ||
+ | result = op.applyAsInt(result, | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | // 두개의 double 값을 연산하여 double값을 리턴 | ||
+ | private static double maxOrMinAvg(DoubleBinaryOperator op) { | ||
+ | double result = (list[0].getMath() + list[0].getEng()) / 2.0; | ||
+ | for(Student s : list) { | ||
+ | result = op.applyAsDouble(result, | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter15; | ||
+ | |||
+ | import java.util.function.Predicate; | ||
+ | |||
+ | public class LambdaEx10 { | ||
+ | |||
+ | // 앞 예제에서 작성한 Student 클래스의 배열 | ||
+ | static Student[] list = { | ||
+ | new Student(" | ||
+ | new Student(" | ||
+ | new Student(" | ||
+ | }; | ||
+ | |||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 과가 컴공인 학생의 영어점수 평균 | ||
+ | double avg = avgEng(t -> t.getMajor().equals(" | ||
+ | System.out.println(" | ||
+ | |||
+ | // 과가 컴공인 학생의 수학점수 평균 | ||
+ | double avg2 = avgMath(t -> t.getMajor().equals(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | private static double avgEng(Predicate< | ||
+ | int count = 0; | ||
+ | int sum = 0; | ||
+ | for (Student student : list) { | ||
+ | // equals 비교 | ||
+ | if (predicate.test(student)) { | ||
+ | count++; | ||
+ | sum += student.getEng(); | ||
+ | } | ||
+ | } | ||
+ | return (double)sum/ | ||
+ | } | ||
+ | |||
+ | private static double avgMath(Predicate< | ||
+ | int count = 0; | ||
+ | int sum = 0; | ||
+ | for (Student student : list) { | ||
+ | // equals() 비교 | ||
+ | if (predicate.test(student)) { | ||
+ | count++; | ||
+ | sum += student.getMath(); | ||
+ | } | ||
+ | } | ||
+ | return (double)sum/ | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||