Function interface of Guava

Keywords: Java

Just reading some blogs is easy to doze off. For this reason, you can learn while recording.

Recently, I've been studying the source code of springfox. I've seen the use of Guava's Function interface many times in its source code. Today, let's learn Guava's Function interface.

First of all, we need to understand a question: what is Guava?

According to the blog, guava is an extension of Java API and provides a more elegant implementation for some common APIs in Java. Guava contains the core library that Google is using in its company's projects.

To use the Function interface of Guava library, you need to import Guava dependencies:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

Back to the topic, Function interface.

First, let's take a look at the source code of this function:

@GwtCompatible
public interface Function<F, T> {
    @Nullable
    T apply(@Nullable F var1);

    boolean equals(@Nullable Object var1);
}

As you can see, this interface provides two abstract methods. We mainly implement the apply method.

Function interface is a generic interface with two generics: F and T. Let's take a specific example to let a class implement the interface, so as to compare the meaning of F and T.

For example:

public class DateFormatFunction implements Function<Date, String> {
    @Override
    public String apply(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        return dateFormat.format(date);
    }
}

You can see that F is actually the type of formal parameter of the apply method. T is the return value type of the apply method.

What are the uses of the Function interface? What is the usage of the Function interface?

Here, we can directly define an anonymous class of Function interface in the main method, and then call the anonymous class object, for example:

public class Main {
    public static void main(String[] args) {
    
        Function<Date, String> function = new Function<Date, String>() {
            @Override
            public String apply(Date date) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                return dateFormat.format(date);
            }
        };
        
        System.out.println(function.apply(new Date())); // output: 29/09/2021
        
    }
}

In addition to defining anonymous classes, there is another way to define an implementation class of a Function interface:

public class DateFormatFunction implements Function<Date, String> {
    @Override
    public String apply(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        return dateFormat.format(date);
    }
}

Then use this implementation class in the main method:

public class Main {
    public static void main(String[] args) {
        DateFormatFunction function = new DateFormatFunction();
        System.out.println(function.apply(new Date()));
    }
}

You can also use polymorphic writing:

public class Main {
    public static void main(String[] args) {
        Function<Date, String> function = new DateFormatFunction();
        System.out.println(function.apply(new Date()));
    }
}

Well, for the following implementation class, in fact, what its apply method does is convert the Date object into a Date formatted String object, which reflects one advantage of using the Function interface: it can convert objects and hide specific details.

public class DateFormatFunction implements Function<Date, String> {
    @Override
    public String apply(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        return dateFormat.format(date);
    }
}

However, the understanding of Guava's Function interface is too simple, and I think it is not enough. I want to know how to use the Function interface in actual development, and what real benefits can the Function interface bring to our program development?

Incidentally, This blog One advantage of using the Function interface is that you can use dependency injection to pass a Function interface to a class, making the code highly cohesive. But what exactly? I need practical examples.

  • The API library of Java also provides a Function interface. Under the java.util.function package, you have to study it to see its relationship with
    What is the difference between Guava's Function interface.

Posted by Jagand on Tue, 28 Sep 2021 19:10:23 -0700