public class FetchVarableList {
public static void main(String[] args) {
Employee e1 = new Employee(1, "Abi", "Fin", 2000);
Employee e2 = new Employee(2, "Chandu", "OPs", 5000);
Employee e3 = new Employee(3, "mahesh", "HR", 8000);
Employee e4 = new Employee(4, "Suresh", "Main", 1000);
List<Employee> empList = new ArrayList<>();
empList.add(e1); empList.add(e2); empList.add(e3); empList.add(e4);
List<String> emps = empList.stream().map(p -> p.getEmpName())
.collect(Collectors.toList());
emps.forEach(System.out::println);
}
}
这关于编程工作正常,但是因为我有声纳违规, 如何更改为lambda表达式?我需要一些帮助
答案 0 :(得分:2)
虽然实现是正确的,但您可能希望使用方法引用:
List<String> emps = empList.stream().map(Employee::getEmpName).collect(Collectors.toList());
这是 squid:S1612 所需的规则。
方法/构造函数引用比使用lambdas更紧凑和可读,因此是首选。