【问题标题】:How to filter key values from map using stream api如何使用流 api 从地图中过滤键值
【发布时间】:2016-06-07 11:54:36
【问题描述】:

我有一张类似的地图

contractAttributesMap=new HashMap<String,WsContractAttribute>();
contractAttributesMap.put("entityDisplayName", new WsContractAttribute("label_contractmap_entityDisplayName","entityDisplayName",WsContractAttribute.AttriibuteType.TEXT));
contractAttributesMap.put("entityNumber",new WsContractAttribute("label_contractmap_entityNumber","entityNumber",WsContractAttribute.AttriibuteType.TEXT));
//other code

我想从键列表中获取值,例如键列表,即键名称列表。

我知道一种方法

private static List<WsContractAttribute> getContractAttributes(Set<String> fields){
          for(String fieldName:fields){
           contractAttributesMap.entrySet()
                    .parallelStream()
                    .filter(e -> e.getKey().contains(fieldName))
                    .map(e -> e.getKey())
                    .collect(Collectors.toList());
          }


      }

但不是循环,我想将此列表本身传递到流 api 中,这将过滤匹配的值并返回列表。 这是可能的。! 提前致谢。

【问题讨论】:

  • 也许你正在寻找类似fields.stream().map(contractAttributesMap::get).collect(toList())的东西?
  • 是的,非常感谢:)

标签: java java-stream


【解决方案1】:

这将由@aioobe 解释完成

  private static List<WsContractAttribute> getContractAttributes(Set<String> fields){
          List<WsContractAttribute> list=fields.stream().map(contractAttributesMap::get).collect(Collectors.toList());
          return list;
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 2017-01-11
    • 2017-04-21
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多