英文:
Java8 Stream: using if/else condition inside a stream
问题
我已经使用Java 8的流操作(Streams)有一段时间了。最近我遇到了一个情况,在迭代流(stream)时需要使用if/else条件。我有一个像这样的列表。我想为列表的每个项目创建一个MetricForAccount类的对象。
```java
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());
我想根据指标名称将不同的项目添加到dimensions列表中。我不太清楚如何在map内部使用if else。有什么建议吗?
<details>
<summary>英文:</summary>
I'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());
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?
</details>
# 答案1
**得分**: 1
```java
List<Dimension> dimensions = new ArrayList<>();
metricNames.stream()
.map(metricName -> {
if (matches(metricName)) return new MetricForAccount().withAccountId(subscriberId)
.withMetricName(metricName)
.withStat(SUM)
.withPeriod(PERIOD)
.withNamespace(AWS_DYNAMO_DB_NAMESPACE)
.withRegion(region)
.withDimensions(dimensions)
.withRange(range);
else return new MetricForAccount().withAccountId(subscriberId)
.withMetricName(metricName)
.withStat(SUM)
.withPeriod(PERIOD)
.withNamespace(AWS_DYNAMO_DB_NAMESPACE)
.withRegion(region)
.withDimensions(otherDimensions)
.withRange(range);
})
.collect(Collectors.toList());
英文:
You can simply extend the callback and use and if/else statement inside this callback.
Like this:
List<Dimension> dimensions = new ArrayList<>();
metricNames.stream()
.map(metricName - > {
if (matches(metricName)) return new MetricForAccount().withAccountId(subscriberId)
.withMetricName(metricName)
.withStat(SUM)
.withPeriod(PERIOD)
.withNamespace(AWS_DYNAMO_DB_NAMESPACE)
.withRegion(region)
.withDimensions(dimensions)
.withRange(range);
else return new MetricForAccount().withAccountId(subscriberId)
.withMetricName(metricName)
.withStat(SUM)
.withPeriod(PERIOD)
.withNamespace(AWS_DYNAMO_DB_NAMESPACE)
.withRegion(region)
.withDimensions(otherDimensions)
.withRange(range);
})
.collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论