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