//原來的匿名內(nèi)部類
@Test
public void test1(){
Comparator comparator = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
};
TreeSet ts = new TreeSet<>(comparator);
}
//Lambda表達式
@Test
public void test2(){
Comparator comparator = (x ,y) -> Integer.compare(x, y);
TreeSet ts = new TreeSet<>(comparator);
}
需求2:獲取當前公司中員工工資大于5000的員工信息
在北湖等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作定制網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)營銷網(wǎng)站建設(shè),北湖網(wǎng)站建設(shè)費用合理。public class Employee {
private String name;
private int age;
private double salary;
public Employee() {
super();
}
public Employee(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
List employees = Arrays.asList(
new Employee("張三", 18 ,9999.99),
new Employee("李四", 38, 5555.99),
new Employee("王五", 50, 6666.66),
new Employee("趙六", 16, 3333.33),
new Employee("田七", 8, 7777.77)
);
@Test
public void test3(){
//獲取年齡大于35歲的員工信息
List employeeList = filterEmployee(employees);
System.out.println("年齡大于35歲的員工信息:");
for (Employee employee : employeeList){
System.out.println(employee);
}
//獲取當前公司中員工工資大于5000的員工信息
List employeeList1 = filterEmployees2(employees);
System.out.println("工資大于5000的員工信息:");
for (Employee employee : employeeList1){
System.out.println(employee);
}
}
獲取當前公司中員工年齡大于35的員工信息
public List filterEmployee(List employeeList){
List emps = new ArrayList<>();
for (Employee emp : employeeList){
if (emp.getAge() >= 35){
emps.add(emp);
}
}
return emps;
}
獲取當前公司中員工工資大于5000的員工信息
public List filterEmployees2(List employeeList){
ArrayList employees = new ArrayList<>();
for (Employee employee : employeeList){
if (employee.getSalary() >= 5000){
employees.add(employee);
}
}
return employees;
}
public interface MyPredicate {
public boolean test(T t);
}
public class FilterEmployeeByAge implements MyPredicate{
@Override
public boolean test(Employee employee) {
return employee.getAge() >= 35;
}
}
public class FilterEmployeeBySalary implements MyPredicate{
@Override
public boolean test(Employee employee) {
return employee.getSalary() >= 5000;
}
}
@Test
public void test4(){
List employeeList = filterEmployee(employees, new FilterEmployeeByAge());
System.out.println("年齡大于35歲的員工信息:");
for (Employee employee : employeeList){
System.out.println(employee);
}
System.out.println("工資大于5000的員工信息:");
List employeeList1 = filterEmployee(this.employees, new FilterEmployeeBySalary());
for (Employee employee : employeeList1){
System.out.println(employee);
}
}
public List filterEmployee(List employeeList, MyPredicate mp){
ArrayList employees = new ArrayList<>();
for (Employee employee : employeeList){
if (mp.test(employee)){
employees.add(employee);
}
}
return employees;
}
public interface MyPredicate {
public boolean test(T t);
}
@Test
public void test5(){
List employeeList = filterEmployee(employees, new MyPredicate() {
@Override
public boolean test(Employee employee) {
return employee.getAge() >= 35;
}
});
System.out.println("年齡大于35歲的員工信息:");
for (Employee employee : employeeList){
System.out.println(employee);
}
List employeeList1 = filterEmployee(employees, new MyPredicate() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() >= 5000;
}
});
System.out.println("工資大于5000的員工信息:");
for (Employee employee : employeeList1){
System.out.println(employee);
}
}
public List filterEmployee(List employeeList, MyPredicate mp){
ArrayList employees = new ArrayList<>();
for (Employee employee : employeeList){
if (mp.test(employee)){
employees.add(employee);
}
}
return employees;
}
@Test
public void test6(){
System.out.println("年齡大于35歲的員工信息:");
List employeeList = filterEmployee(employees, (e) -> e.getAge() >= 35);
employeeList.forEach(System.out::println);
System.out.println("工資大于5000的員工信息:");
List employeeList1 = filterEmployee(employees, (e) -> e.getSalary() >= 5000);
employeeList1.forEach(System.out::println);
}
@Test
public void test7(){
System.out.println("年齡大于35歲的員工信息:");
employees.stream()
.filter((e) -> e.getAge() >= 35)
.forEach(System.out::println);
System.out.println("工資大于5000的員工信息:");
employees.stream()
.filter((e) -> e.getSalary() >= 5000)
.forEach(System.out::println);
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。