1, first define the employee class.
PackageCoffee.how.to.program.early.objects.chapter15; Public classEmployee {PrivateString FirstName; PrivateString LastName; Private Doublesalary; PrivateString Department; //Constructor PublicEmployee (String firstName, String lastName,Doublesalary, String Department) { This. FirstName =FirstName; This. LastName =LastName; This. Salary =salary; This. Department =Department; } //Set FirstName Public voidsetfirstname (String firstName) { This. FirstName =FirstName; } //Get FirstName PublicString Getfirstname () {returnFirstName; } //Set LastName Public voidsetlastname (String lastName) { This. LastName =LastName; } //Get LastName PublicString Getlastname () {returnLastName; } //Set Salary Public voidSetsalary (Doublesalary) { This. Salary =salary; } //Get Salary Public Doublegetsalary () {returnsalary; } //Set Department Public voidsetdepartment (String department) { This. Department =Department; } //Get Department PublicString getdepartment () {returnDepartment; } //return Employee ' s first and last name combined PublicString GetName () {returnString.Format ("%s%s", Getfirstname (), Getlastname ()); } //return a String containing the Employee ' s information@Override PublicString toString () {returnString.Format ("%-8s%-8s%8.2f%s", Getfirstname (), Getlastname (), Getsalary (), getdepartment ()); } //End Method ToString}//End Class Employee
2, Operation class for employee class.
PackageCoffee.how.to.program.early.objects.chapter15;/*** Define several employee instances and add arrays, * Convert the array to list, * Define the comparer based on employee's last name and first name. * Use lambda and stream to manipulate the Employee class. */Importjava.util.Arrays;ImportJava.util.Comparator;Importjava.util.List;Importjava.util.function.Function;Importjava.util.function.Predicate; Public classProcessingemployees { Public Static voidMain (string[] args) {//defining an Employee class arrayemployee[] Employees = { NewEmployee ("Jason", "Red", "the", "IT"), NewEmployee ("Ashley", "Green", 7600, "IT"), NewEmployee ("Matthew", "Indigo", 3587.5, "Sales"), NewEmployee ("James", "Indigo", 4700.77, "Marketing"), NewEmployee ("Luke", "Indigo", 6200, "IT"), NewEmployee ("Jason", "Blue", 3200, "Sales"), NewEmployee ("Wendy", "Brown", 4236.4, "Marketing") }; //Convert to Listlist<employee> list =arrays.aslist (employees); //Print all employee informationSystem.out.println ("Complete Employee List:"); List.stream (). ForEach (System.out::p rintln); System.out.println ("-------------------------------"); //defines a functional interface that returns a Boolean value that specifies the interval of a wage range greater than or equal to 4000 less than or equal to 6000. predicate<employee> Four2sixthousand = e-, (E.getsalary () >= 4000 && e.getsalary () <= 6000); //use stream to filter, sort, and iterate through printing. List.stream (). Filter (Four2sixthousand). Sorted (comparator.comparing (Employee::getsalary)). ForEach (System.ou T::p rintln); //Print Comparator employee information for the maximum wageSystem.out.printf ("%nfirst employee who earns $4000-$6000:%n%s%n", List.stream (). Filter (Four2sixthousand). Min (comparator.comparing (employee::getsalary)). get ()); Function<employee, string> byfirstname =Employee::getfirstname; Function<employee, string> bylastname =Employee::getlastname; //specifying comparator comparison rulesComparator<employee> Lastthenfistcomp =comparator.comparing (bylastname). thencomparing (Byfirstname); //Compare First name with the last name, if the same. System.out.printf ("%nemployees in ascending order by last name then fist name:%n"); List.stream (). Sorted (Lastthenfistcomp). ForEach (System.out::p rintln); //Compare the last name with the first name, if the same. System.out.printf ("%nemployees in descending order by last name and then first:%n"); List.stream (). Sorted (lastthenfistcomp.reversed ()). ForEach (System.out::p rintln); }}
Working with the employee class using a Java 8 lambda expression