Java8 lambda expression multiple list fetch intersection

Source: Internet
Author: User

Java8 Previous procedure:

/*** Intersect from a list of values *@paramLists *@return     */     PublicList<object> Intersection (list<list<object>>lists) {        if(Lists = =NULL|| Lists.size () = = 0){            return NULL; } ArrayList<List<Object>> arrayList =NewArraylist<>(lists);  for(inti = 0; I < arraylist.size (); i++) {List<Object> list =Arraylist.get (i); //Remove an empty collection            if(List = =NULL|| List.size () = = 0) {arraylist.remove (list); I-- ; }        }        if(arraylist.size () = = 0){            return NULL; } List<Object> intersection = Arraylist.get (0) ; //There is only one non-empty collection, and the result is that he        if(arraylist.size () = = 1){            returnintersection; }        //There are multiple non-empty collections that intersect directly         for(inti = 1; I < Arraylist.size ()-1; i++) {Intersection.retainall (Arraylist.get (i)); }        returnintersection; }

Stream-style:

 Public Static List<element> retainelementlist (list<list<element>> elementlists) {              Optional<List<Element>> result = Elementlists.parallelstream ()            null & & (List) elementlist. Size ()! = 0)            , {                a.retainall (b);                 return A;        });         return result.orelse (new arraylist<>());    }

Java8 lambda expression multiple list fetch intersection

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.