Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar violation squid:S1612

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);
    }
}

This about programming is working fine, but in that I have a sonar violation, how to change to lambda expression? I need some help

like image 638
chandrashekar.n Avatar asked Sep 03 '25 15:09

chandrashekar.n


1 Answers

Though the implementation is correct, you might want to use a method reference instead :

List<String> emps = empList.stream().map(Employee::getEmpName).collect(Collectors.toList());

which is what the rule squid:S1612 expects.

Method/constructor references are more compact and readable than using lambdas, and are therefore preferred.

like image 192
Naman Avatar answered Sep 05 '25 07:09

Naman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!