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