是否可以从java

时间:2018-06-21 17:26:23

标签: java java-8

在下面的代码中,我想从Functional接口返回映射。 但是我从下面的代码中得到了错误。预先感谢。

Function< Employee, Map< String, Integer > > funcEmpToString = ( Employee e ) -> {
    Map map = new HashMap< String, Integer >( );
    map.put( e.getName( ), e.getNumber( ) )
    return map;
};

import java.util.*;

public class FunctionExample {

    public static List< String > convertEmpListToNamesList( List< Employee > employeeList, Function< Employee, String > funcEmpToString ) {

        List< String > empNameList = new ArrayList< String >( );
        for ( Employee emp : employeeList ) {
            empNameList.add( funcEmpToString.apply( emp ) );
        }
        return empNameList;
    }

    public static void main( String args[] ) {

        Function< Employee, String > funcEmpToString = ( Employee e ) -> {return e.getName( );};

        List< Employee > employeeList = Arrays.asList( new Employee( "Tom Jones", 45 ),
                new Employee( "Harry Major", 25 ),
                new Employee( "Ethan Hardy", 65 ),
                new Employee( "Nancy Smith", 15 ),
                new Employee( "Deborah Sprightly", 29 ) );

        List< String > empNameList = convertEmpListToNamesList( employeeList, funcEmpToString );
        empNameList.forEach( System.out::println );
    }
}

1 个答案:

答案 0 :(得分:2)

1-是的,有可能。 2-我需要进一步的信息来帮助您,因为您没有告诉我们有关该错误的信息,我认为您在缺少分号时遇到了问题:

map.put(e.getName(), e.getNumber())

以下是您的代码在一个摘要中作为示例进行重写的内容:

package other;

import java.util.*;
import java.util.function.Function;

public class FunctionExample {
    private static class Employee{
        private String name;
        private int number;
        Employee(String name, int number){
            this.name=name;
            this.number = number;
        }

        private int getNumber( ) {

            return number;
        }

        private String getName( ) {

            return name;
        }
    }

    Function< Employee, Map< String, Integer > > funcEmpToString = ( Employee e ) -> {
        Map map = new HashMap< String, Integer >( );
        map.put( e.getName( ), e.getNumber( ) );
        return map;
    };

    public static List< String > convertEmpListToNamesList( List< Employee > employeeList, Function< Employee, String > funcEmpToString ) {

        List< String > empNameList = new ArrayList< String >( );
        for ( Employee emp : employeeList ) {
            empNameList.add( funcEmpToString.apply( emp ) );
        }
        return empNameList;
    }

    public static void main( String args[] ) {

        Function< Employee, String > funcEmpToString = ( Employee e ) -> { return e.getName( );};

        List< Employee > employeeList = Arrays.asList( new Employee( "Tom Jones", 45 ),
                new Employee( "Harry Major", 25 ),
                new Employee( "Ethan Hardy", 65 ),
                new Employee( "Nancy Smith", 15 ),
                new Employee( "Deborah Sprightly", 29 ) );

        List< String > empNameList = convertEmpListToNamesList( employeeList, funcEmpToString );
        empNameList.forEach( System.out::println );
    }
}