如何使用Java8流将列表中的对象与条件映射中的数据相匹配并保存到另一个映射

如何使用Java8流将列表中的对象与条件映射中的数据相匹配并保存到另一个映射,java,java-8,java-stream,Java,Java 8,Java Stream,正在寻找解决方案,如果对象字段以贴图值开始并保存到另一个贴图,则如何将列表中的对象与具有条件的贴图中的数据进行匹配 我有一些数据的地图 Map<String, String> dataMap = new HashMap() dataMap.put("d1", "DATA1") dataMap.put("d2", "DATA2") dataMap.put("d3", "DATA3") 我的代码: Map<String, String> co

正在寻找解决方案,如果对象字段以贴图值开始并保存到另一个贴图,则如何将列表中的对象与具有条件的贴图中的数据进行匹配

我有一些数据的地图

Map<String, String> dataMap = new HashMap()
    dataMap.put("d1", "DATA1")
    dataMap.put("d2", "DATA2")
    dataMap.put("d3", "DATA3")
我的代码:

    Map<String, String> collect2 = dataMap.entrySet().stream()
            .filter({ map -> elements.stream()
                                .anyMatch({ el -> el.getField().startsWith(map.getValue()) })})
            .collect(Collectors.toMap(KEY, VALUE))
Map collect2=dataMap.entrySet().stream()
.filter({map->elements.stream())
.anyMatch({el->el.getField().startsWith(map.getValue())})
.collect(收集器.toMap(键、值))

希望我的问题是对的:

Map<String, String> collect2 = 
    dataMap.entrySet()
          .stream()
          .map(e -> elements.stream()
                            // this will search for the first element of the List matching
                            // the value of the current Entry, if exists
                            .filter(el -> el.getField().startsWith(e.getValue()))
                            .findFirst()
                            // this will create a new Entry having the original key and the
                            // value obtained from the List
                            .map(el -> new SimpleEntry<>(e.getKey(),el.getField()))
                            // if findFirst found nothing, map to a null element
                            .orElse(null))
          .filter(Objects::nonNull) // filter out all the nulls
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

对于给定的输入。

获取异常:线程“main”groovy.lang.MissingMethodException中的异常:没有方法签名:java.util.stream.ReferencePipeline$3.filter()适用于参数类型:(java.lang.Boolean)value:[false]@jhenya-d我必须管理我在没有
DataElement
类的情况下测试的代码,我没有。我将其替换为
String
,并将每个
el.getField()
替换为
el
。这对我很管用。您可以包含
数据元素
类的代码吗?@jhenya-d和关于
groovy.lang.MissingMethodException
——您是用Java还是用groovy编程?这个问题被标记为java,我对Groovy不太熟悉。稍后,我将检查使用
.flatMap(e->elements.stream().filter(…).limit(1.map(…)
时的逻辑,没有
…findFirst()…orElse(null)).filter(Objects::nonNull)
    Map<String, String> collect2 = dataMap.entrySet().stream()
            .filter({ map -> elements.stream()
                                .anyMatch({ el -> el.getField().startsWith(map.getValue()) })})
            .collect(Collectors.toMap(KEY, VALUE))
Map<String, String> collect2 = 
    dataMap.entrySet()
          .stream()
          .map(e -> elements.stream()
                            // this will search for the first element of the List matching
                            // the value of the current Entry, if exists
                            .filter(el -> el.getField().startsWith(e.getValue()))
                            .findFirst()
                            // this will create a new Entry having the original key and the
                            // value obtained from the List
                            .map(el -> new SimpleEntry<>(e.getKey(),el.getField()))
                            // if findFirst found nothing, map to a null element
                            .orElse(null))
          .filter(Objects::nonNull) // filter out all the nulls
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
{d1=DATA1_text1, d2=DATA2_text2}