2. funtions,

Source: Internet
Author: User

2. funtions,

The most crucial distinguish between functional interface invoking and traditional method invoking is that transforming behavious or params it is. however, Transforming behavior is more encapsulated and abstract. the logic methods does not implement predicatbly. we can get any result while invoking method by transforming behavious.

Example of some crucial functional interface:

1. public interface Function <T, R>

The mainly method:

/**     * Applies this function to the given argument.     *     * @param t the function argument     * @return the function result     */    R apply(T t);

Ex:

 list.stream().map(item -> item.toUpperCase()).forEach(item -> System.out.println(item)); 

equals:

 list.stream().map(String::toUpperCase).forEach(System.out::println); 

In String class (invoke object is param):

 public String toUpperCase() { return toUpperCase(Locale.getDefault()); } 

In PrintStream(System.out) class:

 public void println(String x) { synchronized (this) { print(x); newLine(); } } 


And  public interface BiFunction<T, U, R> 
  /**     * Applies this function to the given arguments.     *     * @param t the first function argument     * @param u the second function argument     * @return the function result     */    R apply(T t, U u);

Some functions that have the prefix of Bi is possessing two params and a result.

 

2. public interface Consumer <T>

The mainly method:

  /**     * Performs this operation on the given argument.     *     * @param t the input argument     */    void accept(T t);

 

3. public interface Supplier <T>

The mainly method:

/**     * Gets a result.     *     * @return a result     */    T get();

 

And public interface BinaryOperator <T> extends BiFunction <T, T, T>

  /**     * Returns a {@link BinaryOperator} which returns the lesser of two elements     * according to the specified {@code Comparator}.     *     * @param <T> the type of the input arguments of the comparator     * @param comparator a {@code Comparator} for comparing the two values     * @return a {@code BinaryOperator} which returns the lesser of its operands,     *         according to the supplied {@code Comparator}     * @throws NullPointerException if the argument is null     */    public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {        Objects.requireNonNull(comparator);        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;    }

 

4. public interface Predicate <T>

  /**     * Evaluates this predicate on the given argument.     *     * @param t the input argument     * @return {@code true} if the input argument matches the predicate,     * otherwise {@code false}     */    boolean test(T t);

Ex:

Stream (). filter (item-> item> 2 )...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.