JAVA4_03_Predicate, Function의 결합
Predicate의 결합
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
@SuppressWarnings("unchecked")
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return (Predicate<T>)target.negate();
}
}
and(), or(), negate()
-
and(), or(), negate()로 두 Predicate(두 조건식)를 하나로 결합
-
default메소드
-
and() : && / or() : || / negate() : !
⇒ obj1.and(obj2)
/obj1.or(obj2)
/obj.negate()
Predicate<Integer> p = i -> i < 100;
Predicate<Integer> q = i -> i < 200;
Predicate<Integer> r = i -> i%2 == 0;
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
@SuppressWarnings("unchecked")
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return (Predicate<T>)target.negate();
}
}
and(), or(), negate()로 두 Predicate(두 조건식)를 하나로 결합
default메소드
and() : && / or() : || / negate() : !
⇒ obj1.and(obj2)
/obj1.or(obj2)
/obj.negate()
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)); //f && t || t -> f || t -> t System.out.println(all2.test(2)); //f && (t || t) -> f && t -> f
static isEqual()
- 등가비교를 위한 Predicate의 작성에는 isEqual() 사용
- ✨static 메소드
⇒Predicate.isEqual(obj1)
⇒Predicate.isEqual(obj1).test(obj2)
Predicate<String> p2 = Predicate.isEqual("abc"); boolean result = p2.test("abc22"); System.out.println(result); //false //***한줄로 줄이면!! boolean result2 = Predicate.isEqual("abc").test("abc22"); System.out.println(result2); //false
- 그냥
("abc").equals("abc22")
와 같은 거!
Function의 결합
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() { return t -> t; }
}
- 두 함수를 결합할 경우 : ✨✨(앞 함수의 반환타입 == 뒤 함수의 매개타입)
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() { return t -> t; }
}
ex14_03
import java.util.function.Function;
import java.util.function.Predicate;
public class Ex14_03 {
public static void main(String[] args) {
Function<String, Integer> f = (s) -> Integer.parseInt(s, 16); //문자열 s를 16진수 Integer로
Function<Integer, String> g = (i) -> Integer.toBinaryString(i); //i를 2진수 문자열로
Function<String, String> h = f.andThen(g);
Function<Integer, Integer> h2 = f.compose(g); //== g.andThen(f)
System.out.println(h.apply("FF")); //"FF" -> 255 -> "11111111"
System.out.println(h2.apply(2)); //2 -> "10" -> 16
Function<String, String> f2 = x -> x; //항등 함수
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); //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)); //f && t || t -> f || t -> t
System.out.println(all2.test(2)); //f && (t || t) -> f && t -> f
System.out.println(all.test(150)); //t && t || t -> t
String str1 = "abc";
String str2 = "abc";
Predicate<String> p2 = Predicate.isEqual(str1);
boolean result = p2.test(str2);
System.out.println(result);
//***한줄로 줄이면!!
boolean result2 = Predicate.isEqual(str1).test(str2);
//boolean result2 = str1.equals(str2); 랑 같다.
System.out.println(result2);
}
}
11111111
16
AAA
true
false
true
true
true
Ref
Author And Source
이 문제에 관하여(JAVA4_03_Predicate, Function의 결합), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@lecharl/JAVA403저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)