JAVA9 new features: Using the Pirvate method in the interface to make the default (Java8 interface feature) more concise

Source: Internet
Author: User

Java8 brings many changes, one of which is the default -Modified interface method.

These methods change the interface we know, and now we can define the default implementation method in the interface. the default implementation method differs in that the method can have a method body when the abstract method is decorated with default in the interface, and the method can be implemented without overriding the default adornment method and can be called directly.

If you use the default method heavily in your application interface, you will soon realize that he is not really streamlining the code.

Because you cannot extract the duplicated code from the default in the interface into a new normal method, which conflicts with the default keyword for thin code purposes.

But in java9 this problem was introduced to the private interface methods solved. These newly defined rules allow you to create private adornments in the interface so that we can construct more concise code in the interface.

Using Java9 's Private interface MethodsRefactoring Default Method

code example:

Interface--archive

 public  interface   Archive {List  <article> Getarticles ();  default  list<article> Filterbytitle (String title) { return   Getarticles (). Stream (). filter (article -> Article.gettitle (). Equals (title)  ). Collect (Collectors.tolist ());  default  list<article> Filterbytag (String tag) { return   Getarticles (). Stream (). Filte R (article -> Article.gettags (). Contains (tag)). Collect (Collectors.tolist ()  ); }}

As you can see, archive contains an abstract method-getarticles, and two default methods-Filterbytitle and Filterbytag.

Now, if you look closely at the two default methods, you'll find them almost identical. The only difference is that different predicates are used in the filter method.

There is no need for this repetitive code to be soil. Should make the default code more concise, fortunately Java9 's private interface method can help.

The following is a archive rewritten with private interface methods :

 Public Interfacenewarchive {List<Article>Getarticles (); defaultList<article>Filterbytitle (String title) {returnFilterby (article,article.gettitle (). Equals (title); }  defaultList<article>Filterbytag (String tag) {returnFilterby (article,article.gettags (). Contains (tag)); }  PrivateList<article> Filterby (predicate<article>Tofilterby) {    returngetarticles (). Stream (). Filter (Tofilterby). Collect (Collectors.tolist ()); }}

This is the desired result, and by extracting code other than the predicate, we remove the duplicated content and make the code more readable.

* English Link: deadcoderising

* Original translation, if you have any help, I'd like to

JAVA9 new features: Using the Pirvate method in the interface to make the default (Java8 interface feature) more concise

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.