Javase-java8-lambda Practical Example

Source: Internet
Author: User
Tags instance method iterable

Example one:

Compare with example three, try to use example three

List<invoicequerybean> invoicequerybeanlist =NewArraylist<>(); List<String> invoices =lists.newarraylist (Iterators.transform ( invoicequerybeanlist. Iterator (),New Function<invoicequerybean, string>() {@Nullable @Override PublicString Apply(@Nullable Invoicequerybean input) {if(Stringutils.isnotblank (Input.getloaninvoiceid ())) {returnInput.getloaninvoiceid (); } Else {            return NULL; }    }}));
Removal of empty Iterators.removeif (Invoices.iterator (), Stringutils::isblank);
Example two:
 Public StaticList<personloaninvoicequerypojo> Getinvoicequerypojolist (list<invoicequerybean> Invoicequerybean) {    returnlists.newarraylist (Iterators.transform (invoicequerybean. Iterator (), inputinput = =NULL?NULL : PersonLoanInvoiceQueryPojo.Builder.getInstance (). Addloaninvoiceid (input. Getloaninvoiceid ()). Addusername (Input.getusername ()). Addcertificat Ekind (Input.getcertificatekind ()). Addcertificateno (Input.getcertificateno ()). Addproductname (in Put.getproductname ()). Addmerchantname (Input.getmerchantname ()). Add StoreName (Input.getstorename ()). Addapplydate (Input.getapplydate ()). Addloanamount (Input.getloan                            Amount ()). Addloanperiod (Input.getloanperiod ()). Addloanpurpose (Input.getloanpurpose ()) . Addloandate (Input.getloandate ()). Addrate (Input.getrate ()). Addchannelno (INP Ut.getchannelno ()). Addapprovedate (Input.getapprovedate ()). addreply      (Input.getreply ())                      . Addmarketingcenterid (Input.getmarketingcenterid ()). Build ()));} 
 Public classPersonloaninvoicequerypojoImplementsserializable{Private Static Final LongSerialversionuid = -408985049449365784l; PrivateString Loaninvoiceid; PrivateString userId; PrivateString UserName;  Public Static classBuilder {PrivatePersonloaninvoicequerypojo instance =NewPersonloaninvoicequerypojo (); PrivateBuilder () {} Public StaticBuilder getinstance () {return NewBuilder (); }         Public StaticBuilder getinstance (Personloaninvoicequerypojo instance) {Builder Builder=NewBuilder (); Builder.instance=instance; returnBuilder; }         PublicBuilder Addloaninvoiceid (String loaninvoiceid) { This. Instance.setloaninvoiceid (Loaninvoiceid); return  This; }         PublicBuilder Adduserid (String userId) { This. Instance.setuserid (USERID); return  This; }         PublicBuilder addusername (String userName) { This. Instance.setusername (UserName); return  This; }         PublicPersonloaninvoicequerypojo Build () {return  This. instance; }} setters ();&getters ();}
Example three: Method reference

  There are three main types of method references:

(1) A method reference to a static method (for example: Parseint method in Integer, writing integer::p arseint)

(2) A method reference to an instance method of any type (for example, the length method in String, writing string::length)

(3) A method reference to an instance method of an existing object (as in the following example)

Import com.google.common.collect.Iterators; Import com.google.common.collect.Lists; Listapplyseriallistnew arraylist<>(); List<String> operatornolist = lists.newarraylist (            iterators.transform (applyseriallist  Creditpersonalinfochangeapplyserial::getoperatorno)); This method is called a lambda reference, note that the method referenced by this method does not need ()
Example four:

  Lambad Convert list to map

ImportCom.google.common.collect.Maps; List<QueryUserAppInfoByUserIdListPojo> operatorinfolist =NewArraylist<>(); Map<string, queryuserappinfobyuseridlistpojo>Operatormap=Maps.uniqueindex (operatorinfolist. Iterator (), Queryuserappinfobyuseridlistpojo::getuserid);  Public classQueryuserappinfobyuseridlistpojoImplementsSerializable {Private Static Final LongSerialversionuid = 6876288995978264269L; PrivateString userId;  PublicString getUserId () {return  This. userId; }     Public voidSetuserid (String userId) { This. UserId =userId; }}
Example five:
list=  new arraylist<>();  List, {    if  (Input.getcertificatekind (). Equals ( Enumcertificatekind.resident_identity_card) {        usercertificatemap.put (Pojo.getuserid (), input);    });
Example SIX:

Traversal of the need to use the index of the element, unfortunately, Java8 Iterable does not provide an forEach indexed method, automatically write a to meet their own needs.

Importjava.util.Objects;ImportJava.util.function.BiConsumer;/*** Iterable's tool class*/ Public classIterables { Public Static<E>voidForEach (iterable<?extendsE> elements, Biconsumer<integer,?SuperE>action)        {objects.requirenonnull (elements);        Objects.requirenonnull (action); intindex = 0;  for(E element:elements) {action.accept (index++, Element); }    }}

 Public Static void throws Exception {    list<String> list = Arrays.aslist ("A", "B", "B", "C", "C", "C", "D", "D", "D", "F", "F", " G ");     -SYSTEM.OUT.PRINTLN (Index + "+ str)");

Reference:

Javase-java8-lambda Practical Example

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.