Java 8 的lambda表达式语法与对Stream流的操作已经出来很久了,之前一直没有感觉到有特别好用,直到最近自己在工作开发中心血来潮用了Stream来处理集合,才发现它的强大之处。以前需要许多代码才能完成的集合处理操作,使用Stream和lambda之后,只需要一两行代码就能够轻易完成,而且代码含义也很明确。话不多说,上例子:
场景举例:假设我们需要对一个公司的员工对象按照员工职能进行分类,分类之前不知道一共有几个类别(就这么假设吧。。。)。
这是我们定义的员工类(省略get,set):
public class Employee{
private Integer number;
private String name;
private Integer age;
private Rank rank;
private Gender gender;
public Employee(Integer number, String name, Integer age, Rank rank, Gender gender) {
this.number = number;
this.name = name;
this.age = age;
this.rank = rank;
this.gender = gender;
}
@Override
public String toString() {
return "Employee{" +
"number=" + number +
", name='" + name + '\'' +
", age=" + age +
", rank=" + rank +
'}';
}
public static enum Rank{
MANAGER, PROGRAMMER,TESTER,OPERATOR
}
public static enum Gender{
MALE,FEMALE
}
}
传统的Java代码(我就不写了)一般是先new一个map,key是员工职能,value是一个属于这个职能的员工的集合list,然后对所有员工进行遍历,先判断map中有没有对应的key,有就加到对应的value的list里面;没有就先new一个list,然后把当前员工加到list里面,再把k,v加到map里面,如此循环直到结束。而用了Stream之后,我们看看可以怎么简化:
Map<Employee.Rank, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getRank, Collectors.toList()));
只用了一个groupingBy方法就能取代之前代码的功能,十分强大,效率很高。而且代码的含义也明确:根据Employee的getRank方法返回的值对其进行分组,每组是一个list,结果放到map中。groupingBy方法的第一个参数是分类依据,第二个参数是对分类结果进行处理的函数。
附上完整示例:
public class MainTest {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "e1", 30, Employee.Rank.MANAGER, Employee.Gender.MALE));
employees.add(new Employee(2, "e2", 31, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
employees.add(new Employee(3, "e3", 29, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
employees.add(new Employee(4, "e4", 24, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
employees.add(new Employee(5, "e5", 25, Employee.Rank.PROGRAMMER, Employee.Gender.FEMALE));
employees.add(new Employee(6, "e6", 29, Employee.Rank.PROGRAMMER, Employee.Gender.MALE));
employees.add(new Employee(7, "e7", 25, Employee.Rank.TESTER, Employee.Gender.FEMALE));
employees.add(new Employee(8, "e8", 27, Employee.Rank.TESTER, Employee.Gender.MALE));
employees.add(new Employee(9, "e9", 28, Employee.Rank.OPERATOR, Employee.Gender.MALE));
employees.add(new Employee(10, "e10", 26, Employee.Rank.OPERATOR, Employee.Gender.FEMALE));
System.out.println(employees);
// 对员工按职能进行分类
Map<Employee.Rank, List<Employee>> map = employees.stream()
.collect(Collectors.groupingBy(Employee::getRank, Collectors.toList()));
System.out.println(map);
// 对组内员工按年龄排序
map.values().stream().forEach(MainTest::sortList);
System.out.println(map);
}
// 对员工按年龄大小进行排序
private static void sortList(List<Employee> list) {
Collections.sort(list, (bean1, bean2) -> bean1.getAge().compareTo(bean2.getAge()));
}
}
在上面的代码中,我们也用了stream的forEach方法来进行遍历,forEach方法的参数是一个对遍历的每个元素的进行操作的方法,这里我们传入的是对map.Values的每个list进行集合内部按年龄排序的方法。
运行结果:
所有员工:[Employee{number=1,name='e1',age=30,rank=MANAGER},Employee{number=2,name='e2',age=31,rank=PROGRAMMER},Employee{number=3,name='e3',age=29,rank=PROGRAMMER},Employee{number=4,name='e4',age=24,rank=PROGRAMMER},Employee{number=5,name='e5',age=25,rank=PROGRAMMER},Employee{number=6,name='e6',age=29,rank=PROGRAMMER},Employee{number=7,name='e7',age=25,rank=TESTER},Employee{number=8,name='e8',age=27,rank=TESTER},Employee{number=9,name='e9',age=28,rank=OPERATOR},Employee{number=10,name='e10',age=26,rank=OPERATOR}]
分组之后:{MANAGER=[Employee{number=1,name='e1',age=30,rank=MANAGER}],TESTER=[Employee{number=7,name='e7',age=25,rank=TESTER},Employee{number=8,name='e8',age=27,rank=TESTER}],PROGRAMMER=[Employee{number=2,name='e2',age=31,rank=PROGRAMMER},Employee{number=3,name='e3',age=29,rank=PROGRAMMER},Employee{number=4,name='e4',age=24,rank=PROGRAMMER},Employee{number=5,name='e5',age=25,rank=PROGRAMMER},Employee{number=6,name='e6',age=29,rank=PROGRAMMER}],OPERATOR=[Employee{number=9,name='e9',age=28,rank=OPERATOR},Employee{number=10,name='e10',age=26,rank=OPERATOR}]}
组内年龄排序之后:{MANAGER=[Employee{number=1,name='e1',age=30,rank=MANAGER}],TESTER=[Employee{number=7,name='e7',age=25,rank=TESTER},Employee{number=8,name='e8',age=27,rank=TESTER}],PROGRAMMER=[Employee{number=4,name='e4',age=24,rank=PROGRAMMER},Employee{number=5,name='e5',age=25,rank=PROGRAMMER},Employee{number=3,name='e3',age=29,rank=PROGRAMMER},Employee{number=6,name='e6',age=29,rank=PROGRAMMER},Employee{number=2,name='e2',age=31,rank=PROGRAMMER}],OPERATOR=[Employee{number=10,name='e10',age=26,rank=OPERATOR},Employee{number=9,name='e9',age=28,rank=OPERATOR}]}```