JAVA8 LAMDBA Expression 002

Source: Internet
Author: User
Tags class definition

This article describes the ordering of LAMDBA expressions, which in this case includes a collection of player objects [defined later], which sorts the list's player by the score of each player. Class definition 001 For example the following

public class Sortingplayer {public static void main (string[] args) {list<player> Playerlist = new arraylist<> ();p Layerlist.add (new player ("Black", "White", 9));p Layerlist.add ("John" , "Hello", 2));p Layerlist.add (new player ("Machicel", "Jackson", 7));p Layerlist.add ("Ani", "Hessius", 4)); Playerlist.add ("New Player" ("Mark", "Towns", 3));p Layerlist.add ("Huge", "Nana", 6));} Class Player{private string firstname;private string lastname;private int goals;public Player (string firstName, string LA Stname, int goals) {this.firstname = Firstname;this.lastname = Lastname;this.goals = Goals;} Public String Getfirstname () {return firstName;} public void Setfirstname (String firstName) {this.firstname = FirstName;} Public String Getlastname () {return lastName;} public void Setlastname (String lastName) {this.lastname = LastName;} public int getgoals () {return goals;} public void setgoals (int goals) {this.goals = Goals;}} 
For simplicity, write the code in a class, and the player defines the Firstname,lastname,goals,main method to create a set merge into which several elements are added.

002 The following code is added after the 001main method list

comparator<player> bygoals = comparator.comparing (player::getgoals); System.out.println ("= = Sort by number of goals = =");p Layerlist.stream (). Sorted (bygoals). Map (P, P.getfirstname () + " "+ p.getlastname () +"-"+ p.getgoals ()). ForEach (element, System.out.println (element));


Use the Getter method of the Player object [depending on which field you want to sort.] This example uses goals] to create a comparator-player::getgoals. Then use the mixed lamdba expressions and Streams,foreach (). Displays the sorted collection.


The collection sorting in Java8 adds 3 new features that can greatly improve the development efficiency.

Each is a LAMDBA expression, a method reference, and a stream. For the method reference and stream, which is just a brief introduction, stream can be used in the collection data [collections], which agrees with the elements in the collection for functional operation. Stream does not store data. It allows the acquired collection to have many other functions.

In 002, comparator is generated according to the calculated goals, Player::getgoals. The stream is then generated according to Playerlist . Stream provides the sorted () function, which receives a comparator,comparator when passed to sorted (), and then invokes the map () function, Map uses a LAMDBA expression to spell a firstname, The lastname,&goals string. Finally, because list<player> is iterative, it includes the foreach () method, and the foreach () method agrees to apply an expression or a state group to each element in the collection. In this case. Each element is printed at the command line, because the map () function is already applied in the stream, so the final result is to print the firstname,lastname,&goals of each element in accordance with the goals sequence. For example, see the following:

= = Sort by number of goals ==john Hello-2mark Towns-3ani hessius-4huge nana-6machicel jackson-7black White-9


In addition to the methods that are seen with 002, sort. We are also able to use the sort () method provided by the collections Collection: Collections.sort (), see 003

System.out.println ("= = utilize the Collections.sort () method = ="); Collections.sort (Playerlist, (P1,P2), P1.getlastname (). CompareTo (P2.getlastname ()));p Layerlist.stream (). ForEach (P), {System.out.println (P.getlastname ());});
003 Collections.sort () The first parameter is the set of List<player&gt to be sorted, and the second parameter is the LAMDBA implementation of the sort. In this case, the two parameters passed in are the player objects. Compare their LastName, so this sort will sort the lastname of the collection elements in ascending order. After sorting, the stream is generated and foreach uses the LAMDBA expression to print out the LastName of each element of the sorted collection. For example, see the following:

= = utilize the Collections.sort () method ==hellohessiusjacksonnanatownswhite
Undoubtedly. The LAMDBA expression greatly reduces the code required to sort the collection. And make the code easier to read. This article is about here. Many other features. Tell

PS: This demo sample is quoted from Josh Juneau Java 8 Recipes, 2nd Edition

Mission completed!

O (∩_∩) o~




JAVA8 LAMDBA Expression 002

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.