Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- python
- 중도인출
- 추가납입
- 교보
- 자바스크립트
- 수수료
- 사망
- PythonProgramming
- 인출수수료
- 급성심근경색증
- 심장질환
- 코딩
- 보험
- Vue.js
- Java
- 보험료
- 납입
- 변환
- 프로그래밍
- 뇌출혈
- 웹개발
- jQuery
- javascript
- 파이썬
- 가입
- 특약
- 문자열
- 프론트엔드
- 교보생명
- 리스트
Archives
- Today
- Total
SeouliteLab
[Java/자바] BinaryOperator.maxBy() 메소드 사용 예제 본문
BinaryOperator.maxBy() 메소드 소개
BinaryOperator.maxBy() 메소드는 BinaryOperator 인터페이스의 정적 메소드로, 두 인수 중에서 더 큰 값을 반환하는 BinaryOperator를 생성합니다. 이 메소드는 Comparator를 사용하여 두 인수를 비교합니다.
예제 1: 두 정수 중에서 더 큰 값 찾기
import java.util.function.BinaryOperator;
BinaryOperator<Integer> maxByInt = BinaryOperator.maxBy(Integer::compareTo);
System.out.println("Max value: " + maxByInt.apply(10, 5));
이 예제에서는 BinaryOperator.maxBy()를 사용하여 두 정수 중에서 더 큰 값을 찾았습니다.
예제 2: 두 문자열 중에서 더 큰 값 찾기
import java.util.function.BinaryOperator;
BinaryOperator<String> maxByString = BinaryOperator.maxBy(String::compareTo);
System.out.println("Max value: " + maxByString.apply("apple", "banana"));
이 예제에서는 BinaryOperator.maxBy()를 사용하여 두 문자열 중에서 더 큰 값을 찾았습니다.
예제 3: Comparator.reverseOrder()와 함께 사용하기
import java.util.function.BinaryOperator;
BinaryOperator<Integer> maxByReverse = BinaryOperator.maxBy(Comparator.reverseOrder());
System.out.println("Max value: " + maxByReverse.apply(7, 15));
이 예제에서는 BinaryOperator.maxBy()를 사용하여 Comparator.reverseOrder()와 함께 두 정수 중에서 더 큰 값을 찾았습니다.
예제 4: 두 객체의 특정 속성 비교하기
import java.util.function.BinaryOperator;
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
BinaryOperator<Person> oldestPerson = BinaryOperator.maxBy(Comparator.comparingInt(person -> person.age));
Person person1 = new Person("John", 30);
Person person2 = new Person("Alice", 25);
System.out.println("Oldest person: " + oldestPerson.apply(person1, person2).name);
이 예제에서는 BinaryOperator.maxBy()를 사용하여 두 Person 객체 중에서 나이가 많은 사람을 찾았습니다.
예제 5: null 처리하기
import java.util.function.BinaryOperator;
BinaryOperator<String> maxByStringWithNull = BinaryOperator.maxBy(Comparator.naturalOrder());
System.out.println("Max value: " + maxByStringWithNull.apply("apple", null));
이 예제에서는 BinaryOperator.maxBy()를 사용하여 null 값을 처리하면서 두 문자열 중에서 더 큰 값을 찾았습니다.
예제 6: Optional을 사용하여 두 값 중에서 더 큰 값 찾기
import java.util.Optional;
import java.util.function.BinaryOperator;
BinaryOperator<Integer> maxByOptional = BinaryOperator.maxBy(Integer::compareTo);
Optional<Integer> result = Optional.ofNullable(maxByOptional.apply(20, null));
result.ifPresent(value -> System.out.println("Max value: " + value));
이 예제에서는 BinaryOperator.maxBy()를 사용하여 Optional을 통해 null 값을 처리하고, 두 정수 중에서 더 큰 값을 찾았습니다.
'프로그래밍' 카테고리의 다른 글
[Java/자바] BiPredicate 인터페이스: 예제와 활용 (0) | 2024.03.13 |
---|---|
[Java/자바] BinaryOperator.minBy() 메소드 사용 예제 (0) | 2024.03.13 |
[Java/자바] BinaryOperator를 활용한 예제 (0) | 2024.03.13 |
[Java/자바] 다수의 Consumer 연산을 andThen()으로 처리하는 방법 (0) | 2024.03.13 |
[Java/자바] @FunctionalInterface - 함수형 인터페이스 예제와 활용 (0) | 2024.03.13 |