함수형 인터페이스


 함수형 인터페이스 

 함수 디스크립터 

 기본형 특화 

 Predicate<T>

 T -> boolean 

 IntPredicate, LongPredicate, DoublePredicate 

 Consumer<T>

 T -> void 

 IntConsumer, LongConsumer, DoubleConsumer

 Function<T, R>

 T -> R 

 IntFunction<R>, IntToDoubleFunction, IntToLongFunction, 

 LongFunction<R>, LongToDoubleFunction, LongToIntFunction, 

 DoubleFunction<R>, 

 ToIntFunction<T>, ToDoubleFunction<T>, ToLongFunction<T>  

 Supplier<T> 

 () -> T 

 BooleanSupplier, IntSupplier, LongSupplier, DoubleSupplier

 UnaryOperator 

 T -> T 

 IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator

 BinaryOperator<T> 

 (T, T) -> T 

 IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator

 BiPredicate<L, R> 

 (L, R) -> boolean 

  

 BiConsumer<T, U> 

 (T, U) -> void 

 ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T>

 BiFunction<T, U, R> 

 (T, U) -> R 

 ToIntBiFunction<T, U>, ToLongBiFunction<T, U>, ToDoubleBiFunction<T, U> 








간략화 과정


코드전달

        Comparator<Product> nameComparator = new Comparator<Product>() {
            @Override
            public int compare(Product p1, Product p2) {
                return p1.name.compareTo(p2.name);
            }
        };
        productList.sort(nameComparator);
cs


익명클래스 사용

        productList.sort(new Comparator<Product>() {
            @Override
            public int compare(Product p1, Product p2) {
                return p1.name.compareTo(p2.name);
            }
        });
cs


람다 표현식 사용

        productList.sort((p1, p2)->p1.name.compareTo(p2.name));
cs


메서드 레퍼런스 사용

        productList.sort(Comparator.comparing(Product::getName));
cs









Comparator


reversed

        productList.sort(Comparator.comparing(Product::getName));
cs


thenComparing

        productList.sort(Comparator.comparing(Product::getName).thenComparing(Product::getPrice));
cs








Predicate


기본 사용법

        Predicate<Product> expensiveProduct = (Product p) -> p.price>1000000;
        List<Product> expensiveList = productList.stream().filter(expensiveProduct).collect(Collectors.toList());
cs


negate

        Predicate<Product> cheapProduct = expensiveProduct.negate();
cs


and

        Predicate<Product> expensiveTv = expensiveProduct.and(p -> p.name.equals("TV"));
cs


or

        Predicate<Product> expensiveOrCheapP = expensiveProduct.or(p -> p.price<500000);
cs








Function


andThen

        Function<Integer, Integer> f = x -> x+1;
        Function<Integer, Integer> g = x -> x*2;
        Function<Integer, Integer> h = f.andThen(g);
        
        int result = h.apply(3);
cs


compose

        Function<Integer, Integer> f = x -> x+1;
        Function<Integer, Integer> g = x -> x*2;
        Function<Integer, Integer> h = f.compose(g);
        
        int result = h.apply(3);
cs




'창고' 카테고리의 다른 글

드로이드나이츠(Droid Knights) 2018  (0) 2018.06.10
Googl I/O 2017 Extended in Seoul  (0) 2017.11.10
드로이드나이츠(Droid Knights) 내용  (0) 2017.11.09
드로이드나이츠(Droid Knights)  (0) 2017.11.09
Stream  (0) 2017.10.03

+ Recent posts