多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
https://www.kancloud.cn/hanxt/javacrazy/1641322 ``` import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @Builder @AllArgsConstructor @NoArgsConstructor public class Employee { private String name; private int salary; private String office; } ``` ``` import java.util.*; import java.util.stream.Collectors; public class ExampleEmployee { private static List<Employee> employeeList = new ArrayList<>(); static{ employeeList.add(Employee.builder().name("Matt").salary(5000).office("New Work").build()); employeeList.add(Employee.builder().name("Steve").salary(6000).office("London").build()); employeeList.add(Employee.builder().name("Carrie").salary(3000).office("New Work").build()); employeeList.add(Employee.builder().name("Peter").salary(6000).office("London").build()); employeeList.add(Employee.builder().name("Pat").salary(7000).office("New Work").build()); employeeList.add(Employee.builder().name("Tammy").salary(27000).office("Shang Hai").build()); } public static void main(String[] args) { //anymatch boolean isMatch=employeeList.stream().anyMatch(employee -> employee.getOffice().equals("London")); System.out.println(isMatch); boolean matched=employeeList.stream().allMatch(employee -> employee.getSalary()>2000); System.out.println(matched); //找出工资最高 Optional<Employee> hightSalary=employeeList.stream().max((e1,e2)->Integer.compare(e1.getSalary(),e2.getSalary())); System.out.println(hightSalary); //返回所有姓名列表 List<String> names=employeeList.stream().map(e->e.getName()).collect(Collectors.toList()); System.out.println(names); //list 转化 map Map<String,Employee> employeeMap =employeeList.stream().collect(Collectors.toMap((c->c.getName()),(value->value))); employeeMap.forEach((key,value)-> System.out.println("key:"+key+"---value:"+value)); //统计办公室是new work的个数 long OfficeNum=employeeList.stream().filter(employee -> employee.getOffice()=="New Work").count(); System.out.println(OfficeNum); //list 转化 set Set<String> officeSet =employeeList.stream().map(employee -> employee.getOffice()).distinct().collect(Collectors.toSet()); System.out.println(officeSet); //查找办公室在 new work的员工 Optional<Employee> allMatchedEmployee=employeeList.stream().filter(employee -> employee.getOffice().equals("New Work")).findAny(); System.out.println(allMatchedEmployee); //根据工资升序列出员工列表 List<Employee> sortEmployeeList=employeeList.stream().sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())).collect(Collectors.toList()); System.out.println(sortEmployeeList); //根据员工名字升序列出 List<Employee> sortEmployeeByName=employeeList.stream().sorted((e1,e2)->e1.getName().compareTo(e2.getName())).collect(Collectors.toList()); System.out.println(sortEmployeeByName); //获取员工最高的前两条员工信息 List<Employee> Top2EmployeeList=employeeList.stream().sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())).limit(2).collect(Collectors.toList()); System.out.println(Top2EmployeeList); //获取平均工资 OptionalDouble averageSalary=employeeList.stream().mapToInt(employee -> employee.getSalary()).average(); System.out.println(averageSalary); //查找new york OptionalDouble averageNewYork=employeeList.stream().filter(employee -> employee.getOffice().equals("New Work")).mapToInt(employee -> employee.getSalary()).average(); System.out.println(averageNewYork); } } ```