Lamda expression, lamda

Source: Internet
Author: User

Lamda expression, lamda

// Create a data source for testing in the Person class

Public class Person {

Private String firstName;

Private String lastName;

Private String job;

Private String gender;
Private int salary;

Private int age;

Public Person (String firstName, String lastName, String job,
String gender, int age, int salary ){
This. firstName = firstName;
This. lastName = lastName;
This. gender = gender;
This. age = age;
This. job = job;
This. salary = salary;
}

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 String getJob (){
Return job;
}

Public void setJob (String job ){
This. job = job;
}

Public String getGender (){
Return gender;
}

Public void setGender (String gender ){
This. gender = gender;
}

Public int getSalary (){
Return salary;
}

Public void setSalary (int salary ){
This. salary = salary;
}

Public int getAge (){
Return age;
}

Public void setAge (int age ){
This. age = age;
}
}

 

// Test class

Import java. util .*;
Import java. util. function. Consumer;
Import java. util. stream. Collectors;

Public class Streams {

// 1. No parameter is required. The returned value is 5.
// ()-> 5
//
// 2. Receive a parameter (number type) and return a value of 2 times
// X-> 2 * x
//
// 3. Accept two parameters (numbers) and return their difference
// (X, y)-> x-y
//
// 4. receive two int integers and return their sum
// (Int x, int y)-> x + y
//
// 5. Accept a string object and print it on the console. No value is returned (it looks like void is returned)
// (String s)-> System. out. print (s)

 

  

Public void calc () {// create a set to make a simple sorting output
List <Integer> list = new ArrayList <> ();
List. add (1 );
List. add (23 );
List. add (88 );
List. add (26 );
List. add (64 );

Collections. sort (list, (x, y)-> y-x );
List. forEach (aa)-> System. out. print (aa + ""); // The first output method
List. forEach (System. out: println); // method 2

}

// The first test

// Public static void main (String [] args) {// run and test the calc () method.
// New Streams (). calc ();
//}

 

// Second test

// Public static void main (String [] args ){
// String [] players = {"Rafael Nadal", "Novak Djokovic ",
// "Stanislas Wawrinka", "David Ferrer ",
// "Roger Federer", "Andy Murray ",
// "Tomas Berdych", "Juan Martin Del Potro ",
// "Richard Gasquet", "John Isner"}; // defines an array for sorting and then outputs
//
// Arrays. sort (players, (String s1, String s2)-> (s1.compareTo (s2); // sort the Array
// List <String> lists = Arrays. asList (players); // convert the sorted array into a set.
// Lists. forEach (play)-> System. out. println (play); // output the set content
//}

 

// The third test

// Public static void main (String [] args ){

// List <Person> programmers = new ArrayList <Person> () {// defines a List set.
// Add (new Person ("Jarrod", "Pace", "PHP programmer", "male", 34,155 0 ));
// Add (new Person ("Clarette", "Cicely", "PHP programmer", "female", 23,120 0 ));
// Add (new Person ("Victor", "Channing", "PHP programmer", "male", 32,160 0 ));
// Add (new Person ("Tori", "Sheryl", "PHP programmer", "female", 21,100 0 ));
// Add (new Person ("Osborne", "Shad", "PHP programmer", "male", 32,110 0 ));
// Add (new Person ("Rosalind", "Layla", "PHP programmer", "female", 25,130 0 ));
// Add (new Person ("Fraser", "Hewie", "PHP programmer", "male", 36,110 0 ));
// Add (new Person ("Quinn", "Tamara", "PHP programmer", "female", 21,100 0 ));
// Add (new Person ("Alvin", "Lance", "PHP programmer", "male", 38,160 0 ));
// Add (new Person ("Evonne", "Shari", "PHP programmer", "female", 40,180 0 ));
// Add (new Person ("Jack", "Tgasd", "PHP programmer", "female", 42,160 0 ));
// Add (new Person ("Rose", "Ebacr", "PHP programmer", "female", 36,160 0 ));
// Add (new Person ("Smith", "Ftaaw", "PHP programmer", "female", 21,160 0 ));
// Add (new Person ("Dove", "Drace", "PHP programmer", "female", 19,160 0 ));
// Add (new Person ("Lamda", "Amabg", "PHP programmer", "female", 53,160 0 ));
// Add (new Person ("Grails", "Bhacy", "PHP programmer", "female", 42,160 0 ));
//};

 


// System. out. println ("the following figure shows more than 1400 employees with a monthly salary :");
// Programmers. stream ()
//. Filter (p)-> (p. getSalary ()> 1400) // filter all employees whose salaries exceed 1400.
//. ForEach (p)-> System. out. printf ("% s-% s;", p. getFirstName (), p. getLastName ()));

    

// System. out. println ();
// System. out. println ("employees with a monthly salary equal to 1000 are listed below :");
// Programmers. stream ()
//. Filter (p)-> ("Osborne". equals (p. getFirstName () // filter the last name is Osborne
//. ForEach (p)-> System. out. println (p. getFirstName () + p. getLastName ()));

    

// System. out. println ();
// System. out. println ("employees with salaries equal to $1600 are sorted by age: from large to small by default ");
// List <Person> pg = programmers. stream (). filter (pgs-> 1600 = pgs. getSalary ())

//. Sorted (aud1, aud2)-> aud2.getAge (). compareTo (aud1.getAge () // 1. sort by comparison Function

// 2. The minus signs added before the comparison function can be sorted in descending order. sorted (aud1, aud2)->-(aud2.getAge (). compareTo (aud1.getAge ())))
//. Collect (Collectors. toList ());
// Pg. forEach (p)-> System. out. println (p. getFirstName () + "-" + p. getLastName () + "" + p. getSalary () + "" + p. getAge ()));

//}

 

 

}

 

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.