繁体   English   中英

Java:创建 HashMaps ArrayList 的副本,但仅包含某些键

[英]Java: create copy of ArrayList of HashMaps but include only certain keys

考虑以下数据结构:

ArrayList<HashMap<String, String>> entries = new ArrayList<>();

ArrayList<String> keyNamesToInclude = new ArrayList<>();

此代码创建条目的副本但哈希映射仅包含 keyNamesToInclude 中的键:

ArrayList<HashMap<String, String>> copies = new ArrayList<>();

for (HashMap<String, String> entry: entries) {
  HashMap<String, String> copy = new HashMap<>();
  for (String keyName: keyNamesToInclude) {
    copy.put(keyName, entry.get(keyName));
  }
  copies.add(copy);
}

如何以一种功能性的方式使用 Streams 来创建它?

最好将keyNamesToInclude转换为Set以方便查找键。

然后使用List::stream获取Stream<HashMap>并为每个映射获取其条目的过滤流,重新收集到新映射中并相应地列出。

Set<String> keys = new HashSet<>(keyNamesToInclude); // removes possible duplicates
List<Map<String, String>> copies = entries.stream() // Stream<HashMap>
    .map(m -> m.entrySet()
        .stream()
        .filter(e -> keys.contains(e.getKey()))
        .collect(Collectors.toMap(
            Map.Entry::getKey, Map.Entry::getValue
        ))
    )
    .collect(Collectors.toList());

如果在copies具有ListMap具体实现非常重要,即使Collectors.toList()返回ArrayListCollectors.toMap返回HashMap ,也可能需要转换或特殊形式的收集Collectors.toMap

// casting
ArrayList<HashMap<String, String>> copies2 = (ArrayList) entries.stream() // Stream<HashMap>
    .map(m -> (HashMap<String, String>) m.entrySet()
        .stream()
        .filter(e -> keys.contains(e.getKey()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
    )
    .collect(Collectors.toList());

// special collectors
// toMap(keyMapper, valueMapper, mergeFunction, mapFactory)
// toList -> toCollection(ArrayList::new)
ArrayList<HashMap<String, String>> copies3 = entries.stream() // Stream<HashMap>
    .map(m -> m.entrySet()
        .stream()
        .filter(e -> keys.contains(e.getKey()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, HashMap::new))
    )
    .collect(Collectors.toCollection(ArrayList::new));

您可以执行以下操作:

Set<String> setNamesToInclude = new HashSet<>(keyNamesToInclude);
List<Map<String, String>> copies = entries.stream()
    .map(hm -> hm.keySet()
        .stream()
        .filter(setNamesToInclude::contains)
        .collect(Collectors.toMap(Function.identity(), hm::get))
    )
    .collect(Collectors.toList());

也许使用 hashmap 而不是 list contains 的更有效的解决方案。

List<HashMap<String, String>> copies = maps.stream().map(e -> {
            HashMap<String, String> copy = new HashMap<>();
            keys.forEach(k -> {
                String value = e.get(k);
                if (value != null) 
                    copy.put(k, e.get(k));
            });
            return copy;
        }).collect(Collectors.toList());

您可以执行以下操作(伪代码):

return entries.stream()
  .map(hashmap -> {
    return hashmap.stream()
      .filter((k,v) -> keyNamesToInclude.contains(k))
      .collect(toMap());
  })
  .collect(toList());

我建议使用Set来包含keysToBeIncluded因为这样效率更高一些。 这也会从最终列表中过滤掉空地图。

List<Map<String, String>> entries = List.of(
        Map.of("1", "one", "2", "two", "3", "three"),
        Map.of("2", "two", "3", "three"), Map.of("1", "one"),
        Map.of("6", "six", "7", "seven"));

entries.forEach(System.out::println);

Set<String> keyNamesToInclude = Set.of("2", "3", "6");
  • 流式传输地图的原始列表
  • 对于每个地图,过滤所需的键
  • 构建新地图
  • 过滤器以允许非空映射。
  • 创建列表
List<Map<String, String>> copies = entries.stream()
        .map(m -> m.entrySet().stream().filter(
                e -> keyNamesToInclude.contains(e.getKey()))
                .collect(Collectors.toMap(Entry::getKey,
                        Entry::getValue))).filter(m->!m.isEmpty())
        .toList();  // java 16 - can be replaced with a collector

copies.stream().forEach(System.out::println);

打印四行源代码和三行结果

{3=three, 2=two, 1=one}
{3=three, 2=two}
{1=one}
{7=seven, 6=six}

{2=two, 3=three}
{2=two, 3=three}
{6=six}

暂无
暂无

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

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