Java8 Stream: 在流(Stream)内部使用if/else条件

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

Java8 Stream: using if/else condition inside a stream

问题

  1. 我已经使用Java 8的流操作Streams有一段时间了最近我遇到了一个情况在迭代流stream时需要使用if/else条件我有一个像这样的列表我想为列表的每个项目创建一个MetricForAccount类的对象
  2. ```java
  3. private List<String> metricNames = ImmutableList.of("ReadConsumedCapacity",
  4. "WriteConsumedCapacity", "SuccessfulRequestLatency", "ReturnedItemCount");
  1. List<Dimension> dimensions = new ArrayList<>();
  2. metricNames.stream()
  3. .map(metricName -> new MetricForAccount().withAccountId(subscriberId)
  4. .withMetricName(metricName)
  5. .withStat(SUM)
  6. .withPeriod(PERIOD)
  7. .withNamespace(AWS_DYNAMO_DB_NAMESPACE)
  8. .withRegion(region)
  9. .withDimensions(dimensions)
  10. .withRange(range))
  11. .collect(Collectors.toList());

我想根据指标名称将不同的项目添加到dimensions列表中。我不太清楚如何在map内部使用if else。有什么建议吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;ve been using the Java 8 Streams for a while. I came across a situation where I need to use if/else condition while iterating a stream. I have a list like this. I want to create an object of MetricForAccount class for each item of the list

private List<String> metricNames = ImmutableList.of("ReadConsumedCapacity",
"WriteConsumedCapacity", "SuccessfulRequestLatency", "ReturnedItemCount");

List<Dimension> dimensions = new ArrayList<>();

metricNames.stream()
.map(metricName -> new MetricForAccount().withAccountId(subscriberId)
.withMetricName(metricName)
.withStat(SUM)
.withPeriod(PERIOD)
.withNamespace(AWS_DYNAMO_DB_NAMESPACE)
.withRegion(region)
.withDimensions(dimensions)
.withRange(range))
.collect(Collectors.toList());

  1. I want add different item to the dimensions list based on the metric name. I am not sure how can I use if else inside the map. Any suggestion?
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. ```java
  6. List<Dimension> dimensions = new ArrayList<>();
  7. metricNames.stream()
  8. .map(metricName -> {
  9. if (matches(metricName)) return new MetricForAccount().withAccountId(subscriberId)
  10. .withMetricName(metricName)
  11. .withStat(SUM)
  12. .withPeriod(PERIOD)
  13. .withNamespace(AWS_DYNAMO_DB_NAMESPACE)
  14. .withRegion(region)
  15. .withDimensions(dimensions)
  16. .withRange(range);
  17. else return new MetricForAccount().withAccountId(subscriberId)
  18. .withMetricName(metricName)
  19. .withStat(SUM)
  20. .withPeriod(PERIOD)
  21. .withNamespace(AWS_DYNAMO_DB_NAMESPACE)
  22. .withRegion(region)
  23. .withDimensions(otherDimensions)
  24. .withRange(range);
  25. })
  26. .collect(Collectors.toList());
英文:

You can simply extend the callback and use and if/else statement inside this callback.
Like this:

  1. List&lt;Dimension&gt; dimensions = new ArrayList&lt;&gt;();
  2. metricNames.stream()
  3. .map(metricName - &gt; {
  4. if (matches(metricName)) return new MetricForAccount().withAccountId(subscriberId)
  5. .withMetricName(metricName)
  6. .withStat(SUM)
  7. .withPeriod(PERIOD)
  8. .withNamespace(AWS_DYNAMO_DB_NAMESPACE)
  9. .withRegion(region)
  10. .withDimensions(dimensions)
  11. .withRange(range);
  12. else return new MetricForAccount().withAccountId(subscriberId)
  13. .withMetricName(metricName)
  14. .withStat(SUM)
  15. .withPeriod(PERIOD)
  16. .withNamespace(AWS_DYNAMO_DB_NAMESPACE)
  17. .withRegion(region)
  18. .withDimensions(otherDimensions)
  19. .withRange(range);
  20. })
  21. .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年6月29日 04:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/62627894.html
匿名

发表评论

匿名网友

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

确定