Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lambda 使用HashMap<;字符串,可运行>;避免重复方法_Lambda_Java 8_Hashmap - Fatal编程技术网

Lambda 使用HashMap<;字符串,可运行>;避免重复方法

Lambda 使用HashMap<;字符串,可运行>;避免重复方法,lambda,java-8,hashmap,Lambda,Java 8,Hashmap,问候语,我有一些定义如下的功能: @RequestMapping(value = "/getWeek", method = RequestMethod.GET) public ResponseEntity<?> getAvgWeek(BigInteger id) { List<TimePeriodCalc> result = Calc.getWeek(id); return new ResponseEntity<>(result, HttpSt

问候语,我有一些定义如下的功能:

@RequestMapping(value = "/getWeek", method = RequestMethod.GET)
public ResponseEntity<?> getAvgWeek(BigInteger id) {
    List<TimePeriodCalc> result = Calc.getWeek(id);
    return new ResponseEntity<>(result, HttpStatus.OK);
}
或:

等等


谢谢。

假设
getWeek
getMonth
等方法都采用相同的单个参数(根据您的评论,一个
biginger
),您可以创建一个
映射
映射字符串到
函数
,并用静态方法引用填充此映射

Map<String, Function<BigInteger, List<TimePeriodCalc>>> myMap = new HashMap<>();
myMap.put("week", Calc::getWeek);
myMap.put("month", Calc::getMonth)
...

关于您的评论:Java只有一个参数的for函数,还有两个参数,即x->y和(x,y)->z。不可能使用具有两个以上参数的AFAIK函数或方法引用。如果必须传递两个以上的参数,作为一种解决方法,可以使用,不接受参数,但访问其范围内的变量。但是,这需要在方法中定义
映射

@RequestMapping(value = "/getAny", method = RequestMethod.GET)
public ResponseEntity<?> getAvgAny(String period, BigInteger id, many other parameters) {    
    Map<String, Supplier<List<TimePeriodCalc>>> myMap = new HashMap<>();
    myMap.put("week",  () -> Calc.getWeek(id and many other parameters));
    ...
    List<TimePeriodCalc> result = myMap.get(period).get();
    return new ResponseEntity<>(result, HttpStatus.OK);
}

如前所述,您还可以定义自己的接口,提供一个具有两个以上参数的单一函数,然后使用lambda实现该方法并将这些参数映射到您想要调用的函数,创建这些函数的映射

interface MyInterface {
    List<TimePeriodCalc> getResult(BigInteger id, some more stuff);
}

Map<String, MyInterface> myMap = new HashMap<>();
myMap.put("week", (id, more, stuff) -> Calc.getweek(id, more, stuff));
...
List<TimePeriodCalc> test = myMap.get("week").getResult(id, more, stuff);
接口MyInterface{
列出getResult(BigInteger id,更多内容);
}
Map myMap=newhashmap();
myMap.put(“week”,(id,more,stuff)->Calc.getweek(id,more,stuff));
...
List test=myMap.get(“week”).getResult(id,more,stuff);

问题是什么?我不想使用switch case来选择要执行的函数,我只想做如下事情:
result=myMap.get(“week”)//计算周平均值
或者它可以是使用lambda表达式或至少是java-8样式的任何东西。谢谢。
一些参数是什么…
?这很重要,因为您可能需要一个
映射
,但是
函数
只能接受一个参数(而
双函数
可以接受两个,但不能更多)。我只有一个参数
biginger building\u id
@KhaNguyễ这是一个,你不必删除它。嗨,托比亚斯,谢谢你的回答,这解决了我的问题。顺便说一句,如果我的函数需要超过2个参数怎么办?使用哈希映射无法完成?当然,您也可以创建自己的接口,用指定的参数定义函数。@Holger谢谢,我不知道这一点。不知道为什么,但我实际上认为您不能使用具有两个以上参数的
lambda
。除了按照@Holger的建议创建自己的函数接口(顺便说一句,这是一个不错的选项),如果出于某种原因您不能或不想这样做,您可以使用currified函数,即对于类型为
a
的3参数函数,
B
C
以及返回类型
D
,您可以使用
函数myFunction=a->B->C->yourMethod(a,B,C)
,其中
yourMethod
是一种方法,它接收类型为
a
B
C
的3个参数,并返回类型为
D
的对象。语法很麻烦,但允许使用很好的函数编程技术
result = myMap.get("year");
Map<String, Function<BigInteger, List<TimePeriodCalc>>> myMap = new HashMap<>();
myMap.put("week", Calc::getWeek);
myMap.put("month", Calc::getMonth)
...
@RequestMapping(value = "/getAny", method = RequestMethod.GET)
public ResponseEntity<?> getAvgAny(String period, BigInteger id) {
    List<TimePeriodCalc> result = myMap.get(period).apply(id)
    return new ResponseEntity<>(result, HttpStatus.OK);
}
@RequestMapping(value = "/getAny", method = RequestMethod.GET)
public ResponseEntity<?> getAvgAny(String period, BigInteger id, many other parameters) {    
    Map<String, Supplier<List<TimePeriodCalc>>> myMap = new HashMap<>();
    myMap.put("week",  () -> Calc.getWeek(id and many other parameters));
    ...
    List<TimePeriodCalc> result = myMap.get(period).get();
    return new ResponseEntity<>(result, HttpStatus.OK);
}
Map<String, Function<Object[], List<TimePeriodCalc>>> myMap = new HashMap<>();
myMap.put("week",  p -> Calc.getWeek((BigInteger) p[0], (Some) p[1], (Other) p[2], (Class) p[3]));
...

@RequestMapping(value = "/getAny", method = RequestMethod.GET)
public ResponseEntity<?> getAvgAny(String period, BigInteger id, many other parameters) {    
    Object[] params = new Object[] {id and  many other parameters};
    List<TimePeriodCalc> result = myMap.get(period).apply(params);
    return new ResponseEntity<>(result, HttpStatus.OK);
}
interface MyInterface {
    List<TimePeriodCalc> getResult(BigInteger id, some more stuff);
}

Map<String, MyInterface> myMap = new HashMap<>();
myMap.put("week", (id, more, stuff) -> Calc.getweek(id, more, stuff));
...
List<TimePeriodCalc> test = myMap.get("week").getResult(id, more, stuff);