2017-11-04 5 views
2
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); 
    } 
} 

프로그래밍에 대한이 잘 작동하지만 내가이 수중 음파 탐지기 위반이, 에서는 람다 식으로 변경하려면? 나는 구현이 정확하지만, 대신 메서드 참조를 사용 할 수 있습니다소나 위반 오징어 : S1612

+2

.map (Employee :: getEmpName) –

+0

감사합니다. –

답변

3

도움을 필요

어떤 규칙 squid:S1612 예상하는 것입니다
List<String> emps = empList.stream().map(Employee::getEmpName).collect(Collectors.toList()); 

.

메소드/생성자 참조는 람다를 사용하는 것보다 더 간단하고 읽기 쉽기 때문에 선호됩니다.

+1

을 사용하고 있습니다 만 이것은 정적 메서드에만 적용됩니다. – dragonalvaro