From Groovy to Java 8

Source: Internet
Author: User
Tags abstract assert closure final

Groovy developers have long known the new concepts and new language constructs in Java 8. Of the enhancements that are about to be introduced in the new Java version, groovy has been available a few years ago. From the new syntax for functional programming styles to lambdas expressions, collection streaming, and the use of method references as first-class citizens, groovy developers have a congenital advantage in future Java code writing. This article focuses on the similarities between Groovy and Java 8 and explains how Java 8 interprets the familiar concepts in groovy.

Let's talk about functional programming style, how to use functional programming in groovy, and how the concept of Java 8 provides a better functional programming style.

Closures (Closures) may be the best example of functional programming in groovy. From the internal structure, the closure in groovy is just a functional interface implementation. A functional interface is any interface that requires only one method to be implemented. By default, groovy's closure implements a functional interface called "callable", which implements the "call" method of this interface.

def closure = {
       "called"
} 
assert closure instanceofjava.util.concurrent.Callable
assert closure () = = "called"

By converting the type of closure, we can let groovy implement other functional interfaces.

Public interface Function {
def apply ();
}  
 
def closure = {
        "Applied"
} as function 
 
assert closure instanceof function
assert closure.apply () = = "Applied"

The idea of closures and functional programming is well introduced in Java 8. Functional interfaces are extremely important in the upcoming version of Java, because implicit implementations are provided in Java 8 for the newly introduced lambda functional interface.

We can understand and use lambda functions as closures in groovy. Implementing the Callable interface in Java 8 is as simple as a closure in groovy.

Callable callable = ()-> 

"called";
Assert Callable.call () = = "called";

You need to be particularly aware that Java 8 provides an implied return statement for a single line of lambda functions, and later groovy also borrows this concept. In the future, groovy will also provide implicit implementations for a single abstract method (similar to those provided by Java 8). This feature allows you to use the properties and methods of an instance without having to completely derive the closures specific subclass object.

Abstract class Webflowscope {
       private static final Map Scopemap = [:] 
 
Abstractdefgetattribute (def name); 
 
Publicdef put (key, Val) {
Scopemap[key] = val
getattribute (key)
       }  
 
protected Map Getscope () {
Scopemap
       }
}  
 
webflowscope closure = {Name->
       "Edited_${scope[name]}"
} 
 
Assert Closure Instanceofwebflowscope
assert closure.put ("attribute", "val") = = "Edited_val"

Java 8 presents a similar concept for a functional interface with an interface default method, the new concept of Java, "interface default method." They would like to use this concept to improve the core APIs without violating the interface implementation Protocol (the Implementation protocol established in the previous version of Java).

When you force a lambda function into an interface, they can also use the default method of the interface. That is, a robust API can be built into an interface that enables developers to use these APIs without having to change the type or specification of the types.

Public interface Webflowscope {
        static final Map scopemap = new HashMap (); 
 
        Object GetAttribute (object key); 
 
Default public object is put (object key, Object val) {
scopemap.put (key, Val);
                return getattribute (key);
        } 
 
Default Map Getscope () {return
                Scopemap
        }} 
 
Static final Webflowscope scope = (Object key)-> 
"Edited_" + scope.getscope (). get (key);
Assert Scope.put ("attribute", "val") = = "Val";

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.