일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 티스토리챌린지
- xv6
- mariadb
- leetcode
- 코딩테스트준비
- 비주기신호
- tcp 프로토콜
- 데이터 전송
- 순서번호
- i-type
- git merge
- 프레임 구조
- 플로이드워셜
- 오류검출
- reducible
- IEEE 802
- 그리디 알고리즘
- til
- 오블완
- 99클럽
- 스레드
- 서비스 프리미티브
- 우분투db
- well known 포트
- 항해99
- 개발자취업
- 토큰 버스
- 오류제어
- 주기신호
- tcp 세그먼트
- Today
- Total
Unfazed❗️🎯
Predicate의 결합 본문
Predicate의 결합
-and(), or(), negate()로 두 Predicate를 하나로 결합(default 메서드)
Predicate<Integer> p = i -> i < 100;
Predicate<Integer> q = i -> i < 200;
Predicate<Integer> r = i -> i%2 == 0;
Predicate<Integer> notP =p.negate(); // i >= 100
Predicate<Integer> all = notP.and(q).or(r); // 100 <= i && i < 200 || i%2 == 0
Predicate<Integer> all2 = notP.and(q.or(r)); // 100 <= i && (i < 200 || i%2==0)
System.out.println(all.test(2)); // true
System.out.println(all2.test(2)); // false
-등가비교를 위한 Predicate의 작성에는 isEqual()를 사용(static 메서드)
boolean result = Predicate.isEqual(str1).test(str2); // str1.equals(str2);
public static void main(String[] args) {
Function<String, Integer> f = (s) -> Integer.parseInt(s, 16);
Function<Integer, String> g = (i) -> Integer.toBinaryString(i);
Function<String, String> h = f.andThen(g);
Function<Integer, Integer> h2 = f.compose(g);
System.out.println(h.apply("FF")); //"FF" -> 255 -> "11111111"
System.out.println(h2.apply(2)); //2 -> "10" -> 16
Function<String, String> f2 = x->x; //항등 함수 (identity function)
System.out.println(f2.apply("AAA")); //AAA가 그대로 출려됨
Predicate<Integer> p = i->i<100;
Predicate<Integer> q = i->i<200;
Predicate<Integer> r = i-> i%2==0;
Predicate<Integer> notP = p.negate(); // i >= 100
Predicate<Integer> all = notP.and(q.or(r)); //(i>=100)&&(i<200||i%2==0)
System.out.println(all.test(150)); //true
String str1="abc";
String str2="abc";
//str1과 str2가 같은지 비교한 결과를 반환
Predicate<String> p2 = Predicate.isEqual(str1);
//boolean result = str1.equals(str2);
boolean result = p2.test(str2);
System.out.println(result);
}
output:
11111111
16
AAA
true
true
'Java' 카테고리의 다른 글
Collection.toArray(new T[0]) vs .toArray(new T[size]) / 컬렉션을 배열로 변환하는 두 가지 방법의 성능 비교 (0) | 2024.01.14 |
---|---|
문자열.repeat() 문자열 반복 메서드 (java.lang.String) +내부 소스코드 분석 (0) | 2023.10.27 |
메서드 참조(method reference) (0) | 2023.10.06 |
컬렉션 프레임웍과 함수형 인터페이스 (0) | 2023.10.06 |
java.util.function 패키지 (0) | 2023.10.06 |