This is the 12th day of my participation in Gwen Challenge

4 ye today to share with friends under java8 four functional interface (Consumer: Supplier, the Function, Predicate) is simple to use

Of course, we have to talk about its functional programming first, after all, this is an important improvement ๐Ÿ˜

Functional interface

A FunctionalInterface is an interface that has only one abstract method in it, marked with the @functionalinterface annotation to indicate that the interface is a FunctionalInterface

annotationsFunctionalInterface

As you can see, there’s nothing here, it’s just a labeled interface.

The big four interface

Consumer

The source code is as follows:

Action: Internally digest the parameter passed in, with no return value

Example:

Consumer<String> consumer=System.out::println;
consumer.accept("Consumer");
// result: "Consumer"
Copy the code

Supplier

The source code is as follows:

Function: Returns a result without passing parameters

Example:

Supplier<String> supplier= () -> "Supplier";
System.out.println(supplier.get());
// Result: "Supplier"
Copy the code

Function

This source code is too long, intercept part of the source code as follows:

And you can see it combines thisconsumerandSupplier๐Ÿ–

Function: according to the parameter passed, return a result, here is both the parameter passed, and return value ๐Ÿ˜

Example:

Function<String,String> func= String::toUpperCase;
System.out.println(func.apply("Function"));
// result: "FUNCTION"
Copy the code

Predicate

This source code is also relatively long, intercept part of the source code as follows:

Returns false or true to check whether the given parameter meets the condition

Example:

Predicate<String> predicate= "predicate"::equals;
System.out.println(predicate.test("predicate"));
// Result: true
Copy the code

As you can see, it’s still very simple

Small example

Here’s another quick example (hashMap, which everyone is familiar with)

    @Test
    public void mergeTest(a){
        String key="java4ye";
        HashMap<String, Integer> map=new HashMap<>();
        map.put(key,1);

        / / write 1
        map.merge(key,2,Integer::sum);

        System.out.println(map.get(key));

        / / write 2
        Integer integer = map.get(key);
        if(integer! =null){
            integer+=2;
            map.put(key,integer);
        }

        System.out.println(map.get(key));
    }
    / / 3
    / / 5
Copy the code

Function () Function (); Function ();

And we get another concept, lambda expressions, through the first way of writing, we can also quickly realize that lambda expressions are actually a shortcut to implement functional interfaces


So this issue will briefly introduce these first ~๐Ÿ˜„

The last

Welcome friends to discuss the question ~

If you think this article is good, please give it a thumbs-up ๐Ÿ˜

Let’s start this unexpected meeting! ~

Welcome to leave a message! Thanks for your support! ใƒพ(โ‰งโ–ฝโ‰ฆ*)o go!!

I’m 4ye and I’ll see you soon next time!!