java8的简单用法

以下为java8的一些基本用法

1.按条件过滤集合(filter):

1
List<Employee> list = employeeList.stream().filter(e -> e.getAge() < 30 && "A1".equal(e.getDept())).collect(Collectors.toList());

2.取集合中某个属性作为新的集合(map):

1
List<String> deptList = employeeList.stream().map(Employee::getDept).collect(Collectors.toList());

3.去重(distinct):

1
2
// 第一个distinct()是把相同对象先去重,第二个distinct()是把取得的值去重
List<String> deptList = employeeList.stream().distinct().map(Employee::getDept).distinct().collect(Collectors.toList());

4.分组(groupingBy):

1
Map<String,List<Employee>> map = employeeList.stream(). collect(groupingBy(Employee:getDept));

5.循环(foreach):

1
2
// 循环赋值
employeeList.foreach(e -> {e.setDept("A1"); e.setAge(25);});