如何将以下代码转换为Lambda表达式:

huangapple go评论76阅读模式
英文:

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&lt;String,String&gt; do(Tr req){
       Map&lt;String,String&gt; res=new HashMap&lt;&gt;();
       List&lt;Spp&gt; list=req.getSome();
       if(null!=list) {
           for (Spp sp : list) {
               if (&quot;op1&quot;.equals(sp.getKey())) {
                   res.put(&quot;op1&quot;, sp.getValue());
               }
               if (&quot;op2&quot;.equals(sp.getKey())) {
                   res.put(&quot;op2&quot;, sp.getValue());
               }
               if (&quot;op3&quot;.equals(sp.getKey())) {
                   res.put(&quot;op3&quot;, sp.getValue());
               }
           }
       }
       return res;
    }
if (&quot;op1&quot;.equals(sp.getKey())) {
      res.put(&quot;op1&quot;, 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&lt;String&gt; KEY_SELECTION = Set.of(&quot;op1&quot;, &quot;op2&quot;, &quot;op3&quot;);

public Map&lt;String, String&gt; doSomething(Tr req) {
    return Optional.ofNullable(req.getSome())
        .orElse(Collections.emptyList())
        .stream()
        .filter(sp -&gt; 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 &lt;T, U&gt; void resolve(T t, U u, BiConsumer&lt;T, U&gt; 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&lt;String, String&gt; do1(Tr req) {
    Map&lt;String, String&gt; res = new HashMap&lt;&gt;();
    List&lt;Spp&gt; list = req.getSome();
    Set&lt;String&gt; operation = Set.of(&quot;op1&quot;, &quot;op2&quot;, &quot;op3&quot;);
    if (null != list) {
      for (Spp sp : list) {
        resolve(res,sp,
            (res1, sp1) -&gt; {
              if (operation.contains(sp.getKey())) {
                res.put(sp.getKey(), sp.getValue());
              }
            });
      }
    }
    return res;
  }

huangapple
  • 本文由 发表于 2020年8月10日 17:19:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定