簡體   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