Java8 for loop changed to stream

Source: Internet
Author: User

Just started with java8, a lot of is to replace for loop, because Java8 launched a powerful stream stream, about the use of a lot of flow, Baidu will be able to search for grammar and so on, so here I just want to give some simple alternative for the example, meaning those on their own to check it.
All right, let's get started. Men have a lot of cards, first initialize some data.

List<man> mans =NewArraylist<>(); Mans.add (NewMan ("001", "Zhang San", Arrays.aslist (NewCard ("ICBC", "9558800001"),NewCard ("ICBC", "9558800002"),NewCard ("CCB", "6227001234")))); Mans.add (NewMan ("002", "John Doe", Arrays.aslist (NewCard ("China Merchants Bank", "6225800002"),NewCard ("CCB", "6227035248")))); Mans.add (NewMan ("003", "Harry", Arrays.aslist (NewCard ("CCB", "6227056547"),NewCard ("Bank of China", "6013832547"),NewCard ("Minsheng Bank", "4074058542")))); Mans.add (NewMan ("004", "Zhao Liu", Arrays.aslist (NewCard ("ICBC", "9558832458"),NewCard ("ICBC", "9558832547"),NewCard ("CCB", "6227032578")))); Mans.add (NewMan ("005", "Sun Seven", Arrays.aslist (NewCard ("Bank of China", "6013825847"),NewCard ("abc", "6228836547"),NewCard ("China Merchants Bank", "6225014582")))); Mans.add (NewMan ("006", "Zhang San", Arrays.aslist (NewCard ("ICBC", "9558832587"),NewCard ("Bank of Communications", "6222814578"),NewCard ("ICBC", "9558865427")));

1, looking for Zhang San man, for is this,

 Public List<man> getbyname (list<man> Mans) {        Listnew arraylist<>();          for (man Man:mans) {            if("Zhang San". Equals (Man.getname ())) {                temp.add (man);            }        }         return  temp;    }

The following improvements are:

 Public List<man> GetByName8 (list<man> Mans) {        return mans.stream (). Filter (M--"Zhang San") . Equals (M.getname ())). Collect (Collectors.tolist ());    

The collection here is comparable to the database table, and the filter corresponds to the where of the database.
2, continue, look for the ID 007 man, id unique, for is this

 Public Man GetById (list<man> Mans) {        to  (man Man:mans) {            if ("007"). Equals (Man.getid ())) {                return man ;            }        }         return NULL ;    }

The following improvements are:

 Public Man GetById8 (list<man> Mans) {        return mans.stream (). Filter (M--"Oo7". Equals ( M.getid ()). FindFirst (). OrElse (null);    }

3, continue, get the name called Zhang San (because of the same name) all the bank cards, here does not discuss the actual business significance, only the technology, haha, with for is the case.

 Public List<card> getallcardbyname (list<man> Mans) {        Listnew arraylist<>();          for (man Man:mans) {            if ("Zhang San". Equals (Man.getname ())) {                Cards.addall ( Man.getcards ());            }        }         return cards;    }

This is how it's improved.

 Public List<card> GetAllCardByName8 (list<man> Mans) {        return mans.stream (). Filter (M- > "Zhang San". Equals (M.getname ())). FlatMap (M- m.getcards (). Stream ())                . Collect (Collectors.tolist ());    }

4, continue, on the condition of 3 to add a ICBC condition, for

 Public List<card> getsomecardbyname (list<man> Mans) {        Listnew arraylist<>();          for (man Man:mans) {            if ("Zhang San". Equals (Man.getname ()))}                {for  (Card card:man.getCards ()) {                    if ("ICBC". Equals (Card.getname ())) {                        Cards.add (card);         }}} return cards;    }

This is how it's improved.

 Public List<card> GetSomeCardByName8 (list<man> Mans) {        return mans.stream (). Filter (M- > "Zhang San". Equals (M.getname ())). FlatMap (M- m.getcards (). Stream ())                , "ICBC". Equals ( C.getname ())). Collect (Collectors.tolist ());    }

5, change the name of Zhang San to new opening three, for, attention will change the source data

 Public List<man> changename (list<man> Mans) {        for  (Mans Man:mans) {            if ("Zhang San". Equals (Man.getname ())) {                man.setname ("New opening three");            }        }         return Mans;    }

Improved:

 Public List<man> ChangeName8 (list<man> Mans) {        return mans.stream (). Peek (M-, {             if ("Zhang San". Equals (M.getname ()))                M.setname ("New opening three");        }). Collect (Collectors.tolist ());    }

Java8 for loop changed to stream

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.