英文:
how to convert the following code as a lambda expression
问题
以下是您要翻译的代码部分:
有若干种方法:
public Map<String,String> do(Tr req){
Map<String,String> res=new HashMap<>();
List<Spp> list=req.getSome();
if(null!=list) {
for (Spp sp : list) {
if ("op1".equals(sp.getKey())) {
res.put("op1", sp.getValue());
}
if ("op2".equals(sp.getKey())) {
res.put("op2", sp.getValue());
}
if ("op3".equals(sp.getKey())) {
res.put("op3", sp.getValue());
}
}
}
return res;
}
if ("op1".equals(sp.getKey())) {
res.put("op1", sp.getValue());
}
重复的部分想要转换为 Lambda 表达式。我不知道要使用哪个函数。
英文:
there are a number of methods:
public Map<String,String> do(Tr req){
Map<String,String> res=new HashMap<>();
List<Spp> list=req.getSome();
if(null!=list) {
for (Spp sp : list) {
if ("op1".equals(sp.getKey())) {
res.put("op1", sp.getValue());
}
if ("op2".equals(sp.getKey())) {
res.put("op2", sp.getValue());
}
if ("op3".equals(sp.getKey())) {
res.put("op3", sp.getValue());
}
}
}
return res;
}
if ("op1".equals(sp.getKey())) {
res.put("op1", sp.getValue());
}
repetitive parts want to convert lambda. I don't know what function to use.
答案1
得分: 1
尝试这个。
static final Set<String> KEY_SELECTION = Set.of("op1", "op2", "op3");
public Map<String, String> doSomething(Tr req) {
return Optional.ofNullable(req.getSome())
.orElse(Collections.emptyList())
.stream()
.filter(sp -> KEY_SELECTION.contains(sp.getKey()))
.collect(Collectors.toMap(Spp::getKey, Spp::getValue));
}
英文:
Try this.
static final Set<String> KEY_SELECTION = Set.of("op1", "op2", "op3");
public Map<String, String> doSomething(Tr req) {
return Optional.ofNullable(req.getSome())
.orElse(Collections.emptyList())
.stream()
.filter(sp -> KEY_SELECTION.contains(sp.getKey()))
.collect(Collectors.toMap(Spp::getKey, Spp::getValue));
}
答案2
得分: 0
我可以建议一种替代方法,因为您要求一个功能接口(大致基于 @saka1029 的回答,他的回答比我的好得多)。
定义一个BiConsumer<T, U> biConsumer函数
private static <T, U> void resolve(T t, U u, BiConsumer<T, U> biConsumer) {
biConsumer.accept(t, u);
}
然后在您的方法中进行更改(顺便说一句,在Java中,do是一个保留关键字,所以不能用作方法名)
public static Map<String, String> do1(Tr req) {
Map<String, String> res = new HashMap<>();
List<Spp> list = req.getSome();
Set<String> operation = Set.of("op1", "op2", "op3");
if (null != list) {
for (Spp sp : list) {
resolve(res, sp,
(res1, sp1) -> {
if (operation.contains(sp.getKey())) {
res.put(sp.getKey(), sp.getValue());
}
});
}
}
return res;
}
英文:
I can suggest an alternative way, as you had asked for a functional interface (roughly based on @saka1029 answer, his answer is much better than mine).
Define a BiConsumer<T, U> biConsumer function
private static <T, U> void resolve(T t, U u, BiConsumer<T, U> biConsumer) {
biConsumer.accept(t, u);
}
And make changes in your method as (BTW, do is a reserved keyword in java so it's not accepted as method name)
public static Map<String, String> do1(Tr req) {
Map<String, String> res = new HashMap<>();
List<Spp> list = req.getSome();
Set<String> operation = Set.of("op1", "op2", "op3");
if (null != list) {
for (Spp sp : list) {
resolve(res,sp,
(res1, sp1) -> {
if (operation.contains(sp.getKey())) {
res.put(sp.getKey(), sp.getValue());
}
});
}
}
return res;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论