处理列表流并仅使用非空值收集到map / ImmutableMap中

sidss

如何处理字符串列表并将其仅对存在值的用户集合为Map或Immutable map

String anotherParam = "xyz";
Map.Builder<String,String> resultMap = ImmutableMap.builder(..)

 listOfItems.stream()
            .filter(Objects::nonNull)
            .distinct()
            .forEach(
                    item -> {
                        final Optional<String> result =     
    getProcessedItem(item,anotherParam);

                        if (result.isPresent()) {

    resultMap.put(item, result.get());
                        }
                    });
        return resultMap.build();

请告诉我们,有没有一种更好的方法可以通过收集来实现这一目标?

丹尼尔·杜布罗夫斯基

如果您有权访问Apache Commons库,则可以使用Pair.class

Map<String, String> resultMap = ImmutableMap.copyof(listOfItems()
    .stream()
    .filter(Objects::nonNull)
    .distinct()
    .map(it -> Pair.of(it, getProcessedItem(it,anotherParam))
    .filter(pair -> pair.getValue().isPresent())
    .collect(toMap(Pair::getKey, pair -> pair.getValue().get())))

但是,做一个特殊的数据类来描述映射项->结果是一个很好的习惯

这是一个示例,创建这样的类:

static class ItemResult(){
    public final String item;
    public final Optional<String> result;

    public ItemResult(String item, Optional<String> result){
        this.item = item;
        this.result = result;
    }

    public boolean isPresent(){
        return this.result.isPresent();
    }

    public String getResult(){
        return result.get();
    }
}

并像这样使用它:

Map<String, String> resultMap = ImmutableMap.copyOf(listOfItems()
    .stream()
    .filter(Objects::nonNull)
    .distinct()
    .map(it -> new ItemResult(it, getProcessedItem(it,anotherParam))
    .filter(ItemResult::isPresent)
    .collect(toMap(ItemResult::item, ItemResult::getResult)))

您可以在此处阅读为什么Google放弃元组和对的想法并且在大多数情况下不使用它们

如果毕竟您不想使用任何其他类,则可以使用Optional的api:

Map.Builder<String,String> resultMap = ImmutableMap.builder(..)

listOfItems.stream()
        .filter(Objects::nonNull)
        .distinct()
        .forEach(item -> getProcessedItem(item,anotherParam)
                         .ifPresent(result -> resultMap.put(item result));
    return resultMap.build();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Java流将多个列表收集到一个列表中?

将对象流收集到 Map 中

如何在 Java 中使用流/lambda 重复调用 0 参数函数并将返回值收集到列表中?

使用VBA收集到Excel中的VBA值

Java流将数组收集到一个列表中

在Java 8中使用Lambda将流收集到HashMap中

将对象列表中的值收集到swift5中的字典中

如何在流中收集到TreeMap中?

使用流从地图和值,并收集到列表

如何将Iterator值收集到50个元素的列表中

有没有一种首选的方式将列表流收集到平面列表中?

java.lang.NoClassDefFoundError:使用GeckoDriver Firefox通过Java中的Selenium出现com / google / common / collect / ImmutableMap错误

线程“main”中的异常 java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap 使用 Selenium Java 时出错

为什么在使用“ flat_map”时需要收集到向量中?

如何使用Java 8 groupingBy收集到不同类型的列表中?

如何将多个值收集到数组中,然后在IN子句中使用数组

Java的8过滤+收集到列表中的NullPointerException

递归搜索记录属性并收集到Haskell的列表中

通过其值过滤Map对象,而无需将结果收集到另一个map中

Apache Spark按DF分组,将值收集到列表中,然后按列表分组

如何将名称和存储在模型中的值收集到C#MVC中的列表中

如何将HashMap的值收集到向量中?

使用Immutablemap番石榴的优势

使用Collator在一行中收集到TreeMap

使用Scrapy将数据收集到CSV文件中

Oracle 使用 LOOP 批量收集到集合中

如何使用CompletableFuture打开列表中的所有结果并将其收集到线程中?

使用遞歸,計算列表中相同元素的數量並將其收集到字典中

如何使用RxJava2筛选列表并将其索引收集到字符串结果中?