小编use*_*322的帖子

将参数传递给hashmap值中的函数

我有一个场景,我在调用hashmap值中的函数,如下所示,

  Map<Character, IntSupplier> commands = new HashMap<>();

            // Populate commands map
           int number=10;
            commands.put('h', () -> funtion1(number) );
            commands.put('t', () -> funtion1(number) );

            // Invoke some command
            char cmd = 'h';
          IntSupplier result=  commands.get(cmd); //How can I pass a parameter over here?

System.out.println(" Return value is "+result.getAsInt());
Run Code Online (Sandbox Code Playgroud)

我的问题是,我可以在获取hashmap值时将参数传递给函数(function1),即使用commands.get(cmd)时.

谢谢.

java java-8

2
推荐指数
1
解决办法
180
查看次数

标签 统计

java ×1

java-8 ×1