티스토리 뷰
(기초)그래서 뭘 배운거야?/Java
JV-64-API-Function : 매개변수 있고, 리턴 값 있고, apply~() 추메 가진 매핑 인페
Soheny.P 2021. 11. 18. 17:40728x90
public class Ex06Student {
//필드 선언
private String name;
private int engScore;
private int mathScore;
//매개변수 있는 생성자
public Ex06Student(String name, int engScore, int mathScore) {
// TODO Auto-generated constructor stub
super();
this.name = name;
this.engScore = engScore;
this.mathScore = mathScore;
}
//Getter만 생성
public String getName() {
return name;
}
public int getEngScore() {
return engScore;
}
public int getMathScore() {
return mathScore;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.ToIntFunction;
public class Ex06Function {
//배열선언
public static List<Ex06Student> list = Arrays.asList(
new Ex06Student("호니쓰", 95, 100),
new Ex06Student("이수이", 100, 100)
);
//메소드 : 이름출력
public static void printString(Function<Ex06Student, String> fn) {
for (Ex06Student ex06Student : list) {
System.out.print(fn.apply(ex06Student)+" ");
}System.out.println();
}
//메소드 : 점수 출력
public static void printInt(ToIntFunction<Ex06Student> fn) {
for (Ex06Student ex06Student : list) {
System.out.print(fn.applyAsInt(ex06Student)+" ");
}System.out.println();
}
public static void main(String[] args) {
System.out.println("[이름]");
printString(t -> t.getName());
System.out.println("[기분]");
printInt(t -> t.getEngScore());
System.out.println("[허기짐]");
printInt(t -> t.getMathScore());
}
}
import java.util.Arrays;
import java.util.List;
import java.util.function.ToIntFunction;
public class Ex07Function1 {
//Function
/*
- 매개변수와 리턴값이 있고, apply~~()메서드를 가진 람다 인터페이스
- 매개변수와 리턴값 매핑시 사용
*/
// 배열선언
public static List<Ex06Student> list = Arrays.asList(
new Ex06Student("호니쓰", 95, 100),
new Ex06Student("이수이", 100, 100)
);
public static double avg(ToIntFunction<Ex06Student> fn) {
int sum = 0;
for (Ex06Student ex06Student : list) {
sum += fn.applyAsInt(ex06Student);
}
double avg = (double) sum / list.size();
return avg;
}
public static void main(String[] args) {
double engAvg = avg(s -> s.getEngScore());
System.out.println("기분 평균 점수 : "+engAvg);
double mathAvg = avg(s -> s.getMathScore());
System.out.println("허기짐 평균 점수 : "+mathAvg);
}
}
728x90
'(기초)그래서 뭘 배운거야? > Java' 카테고리의 다른 글
JV-65-API-Operator : 매개변수 있고, 리턴 값 있고, apply~() 추메 가진 연산 인페 (0) | 2021.11.18 |
---|---|
JV-63-API-Supplier : 매개변수 없고, 리턴 값 있고, get~~() 추메 가진 인페 (0) | 2021.11.18 |
JV-62-API-Consumer : 리턴 값 없는, accept()추메 가진 인페 (0) | 2021.11.18 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- boldtag
- emtag
- 줄글
- br tag
- 합격
- 단락태그
- definition List
- tabletag
- hn태그
- 정보처리기사필기
- 정보처리기사실기
- 긴문장
- marktag
- tablespan
- 93점
- 비전공
- spantag
- hr tag
- pretag
- usemap
- imgtag
- ptag
- 정처기
- 정보처리기사
- ul>li
- 2021년2회
- ol>li
- 2021년42회
- html
- 복습
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함