JAVA8 LAMDBA Expression 002

Source: Internet
Author: User
Tags class definition

This article will describe the ordering of the LAMDBA expression, which contains a collection of player objects [defined later], which sorts the list's player by the score level of each player, class definition 001 as follows

public class Sortingplayer {public static void main (string[] args) {list<player> playerlist = new Arraylist<> ( ;p Layerlist.add ("Black", "White", 9));p Layerlist.add ("John", "Hello", 2);p Layerlist.add (new Player ("Machicel", "Jackson", 7));p Layerlist.add ("Ani", "Hessius", 4);p Layerlist.add ("New player", "Mark", " Towns ", 3));p Layerlist.add (New Player (" 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 where 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 [This example uses goals] to create a comparator-player::getgoals based on the field you want to sort. Then use the mixed lamdba expressions and Streams,foreach () to show the sorted collection.


The collection sorting in Java8 adds 3 new features that can greatly improve the development efficiency. The LAMDBA expression, the method reference, and the stream, respectively. For the method reference and stream, this is a simple introduction, and stream can be used in the collection data [collections], which allows the elements in the collection to function. Stream does not store data, it allows the acquired collection to have more functionality.

In 002, comparator is generated according to the calculated goals, Player::getgoals. The stream is then generated according to Bygoals. Stream provides the sorted () function, which receives a comparator,comparator that has been initialized when passed to sorted (), and then calls the map () function, Map uses a LAMDBA expression to spell a firstname, The lastname,&goals string. Finally, because list<player> is iterative and contains the foreach () method, the foreach () method allows each element in the collection to apply an expression or a state group. In this example, each element is printed at the command line, and since the map () function has already been applied in the stream, the final result is to print the firstname,lastname,&goals of each element that follows the goals sequence. As shown below:

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


In addition to sorting using the method shown in 002, we can also 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, the second parameter is the LAMDBA implementation of the sort, and in this case two parameters are the Player objects, comparing 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, as follows:

= = utilize the Collections.sort () method ==hellohessiusjacksonnanatownswhite
Undoubtedly, the LAMDBA expression greatly reduces the code required to sort the collection. and makes the code easier to read. This article is about here. More features, tell.

PS: This example 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.