stream: function 인터페이스

·2022년 11월 8일
0
  • 추상메소드
    R apply(T t)
    Function<파라미터타입,리턴타입>
    클래스 타입이 오는 거라서 int는 올 수 없음 Integer써줘야함

  • 문법

Function<Integer, Double> fun = new Function<Integer, Double>() {
		@Override
	public Double apply(Integer t) {
			return t/2.0;
	}
};
	double a = fun.apply(5);
	System.out.println("a: "+a);
  • 람다식(추상메소드 생략)
  1. 익명의 내부클래스에서 첫줄 Function<Integer, Double> fun = 가져오기
  2. 오버라이딩한 추상메소드 파라미터 가져오기 Integer t
  3. 오버라이딩한 추상메소드 return 문 가져오기 return t/2.0;
  4. 나머지 출력문 가져오기
Function<Integer, Double> fun=t->t/2.0;
//첫줄 = 파라미터->return문;
double a = fun.apply(5);
System.out.println("a: "+a);
  • Student 타입(학점구하기)
    Student객체를 파라미터로 전달받아 학점을 리턴하는 Function인터페이스를 람다식으로 이용해 만들고 사용해 보세요 . 학점: 90점 이상 "A" 등
    사용될 Student메소드 : getScore()
    생성자 Student(int num,String name,int score){}
Function<Student,Integer> fun=t->t.getScore()/10;
int n=fun.apply(new Student(1,"한소라",99));
switch(n){
  case 10:
  case 9: System.out.println("A"); break;
  case 8: System.out.println("B"); break;
  case 7: System.out.println("C"); break;
  case 6: System.out.println("D"); break;
  case 5:
  case 4:
  case 3:
  case 2:
  case 1:
  case 0: System.out.println("F"); break;
}
profile
웹개발입문자

0개의 댓글