如何使用Java SE 8 Stream API实现这个?

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

How to implement this with the Java SE 8 Stream API?

问题

我已经尝试了以下代码,将其转换为Java 8:

List<Value> values = valueRepository.findByCorp(corpId, type, "NONE");
Map<Integer, List<String>> map = new HashMap<>();

List<Value> tempList = new ArrayList<>();
// 这是有效的代码片段
values.forEach(v -> {
    for (Product product : products) {
        if (!isEditable(map.get(product.getProductId()), v.getId().getCode()) ||
            "TEST".equals(v.getId().getCode())) {
            continue;
        }
        tempList.add(v);
        break;
    }
});

这是我迄今为止尝试的内容:

values.forEach(v -> {
    Optional<ProductDTO> findProduct = products.stream().filter(product ->
        isEditable(map.get(product.getProductId()), v.getId().getCode()) ||
        !"TEST".equals(v.getId().getCode())
    ).findFirst();
    
    if (findProduct.isPresent()) {
        tempList.add(v);
    }
});

但是不知何故它无法正常工作。非常感谢您提供任何帮助。

英文:

I have tried below code to convert into java8 :

List&lt;Value&gt; values = valueRepository.findByCorp(corpId, type, &quot;NONE&quot;);
Map&lt;Integer, List&lt;String&gt;&gt; map = new HashMap&lt;&gt;();

  List&lt;Value&gt; tempList = new ArrayList&lt;&gt;();
//This is working code snippet
    values.forEach(v -&gt; {
  for (Product product : products) {
    if (!isEditable(map.get(product.getProductId()), v.getId().getCode()) ||
        &quot;TEST&quot;.equals(v.getId().getCode())) {
      continue;
    }
    temList.add(v);
    break;
  }
});



 private boolean isEditable(List&lt;String&gt; list, String type) {
    return list != null &amp;&amp; list.size() &gt; 0 &amp;&amp; list.contains(type);
  }

Here is mine stuff what I have tried so far:

    values.forEach(v -&gt; {
        Optional&lt;ProductDTO&gt; findProduct = products.stream().filter(isEditable(map.get(product.getProductId()), v.getId().getCode()) ||
            !&quot;TEST&quot;.equals(v.getId().getCode())).findFirst();
			if(findProduct.isPresent()) {
			  temList.add(v);
			}
  });

But its not working somehow. Any help is highly appreciable.

答案1

得分: 1

错误的逻辑运算符被使用,将 || 更改为 &&

values.forEach(v -> {
    Optional<ProductDTO> findProduct = products.stream().filter(isEditable(map.get(product.getProductId()), v.getId().getCode()) &&
        !"TEST".equals(v.getId().getCode())).findFirst();
    if (findProduct.isPresent()) {
        temList.add(v);
    }
});
英文:

wrong logical operator used, change || to &amp;&amp;:

    values.forEach(v -&gt; {
    Optional&lt;ProductDTO&gt; findProduct = products.stream().filter(isEditable(map.get(product.getProductId()), v.getId().getCode()) &amp;&amp;
        !&quot;TEST&quot;.equals(v.getId().getCode())).findFirst();
        if(findProduct.isPresent()) {
          temList.add(v);
        }
   });

答案2

得分: 0

不使用 isEditable 函数

List<Value> tempList = values.stream()
        .filter(v -> !v.getId().getCode().equals("TEST"))
        .filter(v -> products.stream()
                .map(p -> map.get(p.getProductId()))
                .filter(Objects::nonNull)
                .filter(l -> l.size() > 0)
                .filter(l -> l.contains(v.getId().getCode()))
                .findFirst().isPresent())
        .collect(Collectors.toList());
英文:

without using isEditable function

List&lt;Value&gt; tempList = values.stream()
            .filter(v -&gt; v.getId().getCode() != &quot;TEST&quot;)
            .filter(v -&gt; products.stream()
                    .map(p -&gt; map.get(p.getProductId()))
                    .filter(Objects::nonNull)
                    .filter(l -&gt; l.size() &gt; 0)
                    .filter(l -&gt; l.contains(v.getId().getCode()))
                    .findFirst().isPresent())
            .collect(Collectors.toList());

答案3

得分: -1

使用流式处理将 `value` 的内容进行流式传输并通过将每个值与所有产品进行比较来对其进行筛选使用您的取反的条件您的条件是要跳过/继续 - 当取反时将导致在最终结果中包含一个值的条件

tempList = values.stream()
                .filter(value -> !"TEST".equals(value.getId().getCode()))
                .filter(value -> products.stream()
                        .anyMatch(product -> isEditable(map.get(product.getProductId()), 
                                value.getId().getCode())))
                .collect(Collectors.toList());
英文:

Stream the contents of value and filter them by comparing each value against all products using your (negated) condition. Your condition was to skip/continue - the same when negated would result in a condition to include a value in the final result.

tempList = values.stream()
            .filter(value -&gt; !&quot;TEST&quot;.equals(value.getId().getCode())
            .filter(value -&gt; products.stream()
                    .anyMatch(product -&gt; isEditable(map.get(product.getProductId()), 
                            value.getId().getCode()))
            .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月2日 12:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698757.html
匿名

发表评论

匿名网友

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

确定