繁体   English   中英

如何使用stream api从map中过滤键值

[英]How to filter key values from map using stream api

我有一张地图

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而不是循环,这将过滤匹配的值和返回列表。 这是可能的。!! 提前致谢。

这将由@aioobe解释

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM