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

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

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&#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());


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&lt;Dimension&gt; dimensions = new ArrayList&lt;&gt;();

metricNames.stream()
    .map(metricName - &gt; {
        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());

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:

确定