Java8 new features: Using lambda to process list collections

Source: Internet
Author: User

Java 8 is a new lambda expression that allows us to work with lists in simple, efficient code.

1. Traverse
 Public Static void Main (string[] args) {        List<User> userlist = Lists.newarraylist ();          New User (1L, "Zhang San",);         New User (2L, "John Doe", +);         New User (3L, "Harry", +);        Userlist.add (user1);        Userlist.add (user2);        Userlist.add (User3);         {            System.out.println (User.getname ());        });    

Operation Result:

2. List to map
  Public Static voidMain (string[] args) {List<User> userlist = Lists.newarraylist ();//storing Apple object collectionsUser user1=NewUser (1L, "Zhang San", 24); User User2=NewUser (2L, "John Doe", 27); User User3=NewUser (3L, "Harry", 21);        Userlist.add (user1);        Userlist.add (User2);        Userlist.add (USER3); //ID key, switch to mapMap<long,user> UserMap = Userlist.stream (). Collect (Collectors.tomap (User::getid, A, a, K1, K2),K1));    System.out.println (UserMap); }

Operation Result:

3. Group list: Object elements in list, grouped by an attribute
   Public Static voidMain (string[] args) {List<User> userlist = Lists.newarraylist ();//storing Apple object collectionsUser user1=NewUser (1L, "Zhang San", 24); User User2=NewUser (2L, "John Doe", 27); User User3=NewUser (3L, "Harry", 21); User User4=NewUser (4L, "Zhang San", 22); User User5=NewUser (5L, "John Doe", 20); User User6=NewUser (6L, "Harry", 28);        Userlist.add (user1);        Userlist.add (User2);        Userlist.add (USER3);        Userlist.add (USER4);        Userlist.add (USER5);        Userlist.add (USER6); //group UserList According to the namemap<string, list<user>> groupBy =Userlist.stream (). Collect (Collectors.groupingby (user::getname));    System.out.println (GroupBy); }

Operation Result:

4, Filter: Filter out the set of eligible elements
  Public Static voidMain (string[] args) {List<User> userlist = Lists.newarraylist ();//storing Apple object collectionsUser user1=NewUser (1L, "Zhang San", 24); User User2=NewUser (2L, "John Doe", 27); User User3=NewUser (3L, "Harry", 21); User User4=NewUser (4L, "Zhang San", 22); User User5=NewUser (5L, "John Doe", 20); User User6=NewUser (6L, "Harry", 28);        Userlist.add (user1);        Userlist.add (User2);        Userlist.add (USER3);        Userlist.add (USER4);        Userlist.add (USER5);        Userlist.add (USER6); //Remove the user named Zhang Sanlist<user> filterlist = Userlist.stream (). Filter (User-User.getname (). Equals ("Zhang San") . Collect (Collectors.tolist ()); Filterlist.stream (). ForEach (User-{System.out.println (User.getname ());    }); }

Operation Result:

5. Sum: Sums the data in the collection according to a property
  Public Static voidMain (string[] args) {List<User> userlist = Lists.newarraylist ();//storing Apple object collectionsUser user1=NewUser (1L, "Zhang San", 24); User User2=NewUser (2L, "John Doe", 27); User User3=NewUser (3L, "Harry", 21); User User4=NewUser (4L, "Zhang San", 22); User User5=NewUser (5L, "John Doe", 20); User User6=NewUser (6L, "Harry", 28);        Userlist.add (user1);        Userlist.add (User2);        Userlist.add (USER3);        Userlist.add (USER4);        Userlist.add (USER5);        Userlist.add (USER6); //Remove the user named Zhang San        intTotalage =Userlist.stream (). Maptoint (User::getage). sum (); System.out.println ("And:" +totalage); }

Operation Result:

Java8 new features: Using lambda to process list collections

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.