gwooden_코린이

java_Operator(오퍼레이터)_221205(11일차) 본문

java/11일차

java_Operator(오퍼레이터)_221205(11일차)

gwooden22 2022. 12. 5. 10:20
728x90

Operator(매개변수) + 리턴이 한쌍

package 오퍼레이터;

import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;

public class Sample01 {

	public static void main(String[] args) {
//		매개 변수들에 변수 타입, 리턴 타입을 제네릭을 통해 지정<> 객체 이름 = (
		BinaryOperator<Double> bo = (a, b) -> a + b;
		//BinaryOperator <-- 이항연산자 두개의 값을 가지고 람다식을 실행시킨다.
		
//		apply를 통해 a, b 변수에 값을 추가해준다.
//		bo.apply(3.5, 16.3);
		
		System.out.println(bo.apply(3.5, 16.3));
      }
}


package 오퍼레이터;

import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;

public class Sample01 {

	public static void main(String[] args) {
//		매개 변수들에 변수 타입, 리턴 타입을 제네릭을 통해 지정<> 객체 이름 = (
		BinaryOperator<Double> bo = (a, b) -> a + b;
		//BinaryOperator <-- 이항연산자 두개의 값을 가지고 람다식을 실행시킨다.
		
//		apply를 통해 a, b 변수에 값을 추가해준다.
//		bo.apply(3.5, 16.3);
		
		System.out.println(bo.apply(3.5, 16.3));
		
		
		UnaryOperator<Double> uo = (a) -> a + 10;
		//UnaryOperator <-- 한개 매개변수를 이용한다.
		
		System.out.println(uo.apply(3.14));

	}

}


		//에초에 바이너리 타입을 실수형인 더블로 지정하고 가면 제네릭을 사용할 필요가 없다.
		DoubleBinaryOperator dbo = (a, b) -> a * b;
		
		//바이너리이 타입을 처음에 지정하면 apply또한 지정한 타입에 맞게 사용해야 된다.
		System.out.println(dbo.applyAsDouble(1.4, 2.6));

 


		IntUnaryOperator iuo = (x) -> {
			System.out.println("람다 실행");
			return x+10;
		};
		
		System.out.println(iuo.applyAsInt(10));


package 오퍼레이터;

import java.util.function.IntBinaryOperator;

public class Sample01 {

	public static void main(String[] args) {
		
		IntBinaryOperator inx = (a, b) -> { 
				if(a > b) {
					return a;
			}else {
				return b;
			}
		};
		
		System.out.println(inx.applyAsInt(5, 7));

	}
		
}


(BinaryOperator)단항연산 = 매개변수 1개

(UnaryOperator)이항연산 = 매개변수 2개

 

앞에 자료형 타입을 지정할 수 있다.

ex)

IntBinaryOperator  -> apply할 때도 지정한 자료형 타입에 맞춰서 .applyAsInt

DoubleBinaryOperator -> .applyAsDouble

 

 

불러올 인터페이스  객체이름 = (매개변수) -> 구현 (리턴값);

 

불러올 인터페이스  객체이름 = (매개변수) -> { 구현

            리턴 값;

}

 


package 오퍼레이터;

import java.util.function.IntBinaryOperator;

public class Sample02 {
	
	//정적변수
	//메서드 생성
	static int[] scores = {92, 93, 78};
	
	//단순 반복돌려서 결과만 돌려주는 메서드
	static int maxOrMin(IntBinaryOperator operator) {
		int result = scores[0];
		
		//향상된 for문
		for(int score : scores) {
			result = operator.applyAsInt(score, result);
		}
		
		return result;
	}

	public static void main(String[] args) {
		//람다식이 max라는 변수에 저장됨
		int max = maxOrMin( //maxOrMin <- 메서드호출
				//람다식
				(a, b) -> {
					if(a > b) {
						return a;
					} else {
						return b;
					}
				}
			
			); //메서드 종료
		
		System.out.println("최대값 : " + max);
		
		
		
		
		int min = maxOrMin ( //maxOrMin <- 메서드호출
				//람다식
				(a, b) -> {
					if(a < b) {
						return a;
					} else {
						return b;
					}
				}
			
			); //메서드 종료
		
		System.out.println("최소값 : " + min);

	}

}

728x90

'java > 11일차' 카테고리의 다른 글

java_스트림_221205(11일차)  (0) 2022.12.05
java_andThen/compose_221205(11일차)  (0) 2022.12.05
java_Predicate_221205(11일차)  (0) 2022.12.05
Comments