gpt4 book ai didi

java - 如何迭代两个多重映射并打印文件中的差异?

转载 作者:行者123 更新时间:2023-12-01 09:58:08 25 4
gpt4 key购买 nike

我有两个多重贴图,如下所示:

ListMultimap<String, String> source_multimap = ArrayListMultimap.create();
ListMultimap<String, String> target_multimap = ArrayListMultimap.create();

for (SwiftTagListBlock s : source_tagListBlock) {
Iterator<Tag> sourcetag_iterator = s.tagIterator();

while (sourcetag_iterator.hasNext()) {
Tag tag = (Tag) sourcetag_iterator.next();
source_multimap.put(tag.getName(), tag.getValue());
}
}
for (SwiftTagListBlock t : target_tagListBlock) {
Iterator<Tag> targettag_iterator = t.tagIterator();

while (targettag_iterator.hasNext()) {
Tag tag = (Tag) targettag_iterator.next();
target_multimap.put(tag.getName(), tag.getValue());
}
}

它将为我们提供一对具有单键和多值的键值对。我想像下面这样进行比较:

if(mulimap1.get(key).equals(multimap2.get(key))) 然后比较集合中的值。 if(multimap1.getValues()!=multimap2.getValues()) 然后打印不同的值。

最佳答案

Sets.intersection(source_multimap.keySet(), target_multimap.keySet()).forEach(key -> {
Multiset<String> multiset1 = ImmutableMultiset.copyOf(source_multimap.get(key));
Multiset<String> multiset2 = ImmutableMultiset.copyOf(target_multimap.get(key));
System.out.println(key + " has values different: " + Multisets.union(
Multisets.difference(multiset1, multiset2),
Multisets.difference(multiset2, multiset1)
));
});

示例输出:

source: {a=[1, 1], b=[1]}
target: {a=[1, 2], b=[1, 2], c=[1]}
a has values different: [1, 2]
b has values different: [2]

如果您想包含任一 map 特有的键的差异,请将 Sets.intersection 替换为 Sets.union:

source: {a=[1, 1], b=[1]}
target: {a=[1, 2], b=[1, 2], c=[1]}
a has values different: [1, 2]
b has values different: [2]
c has values different: [1]

关于java - 如何迭代两个多重映射并打印文件中的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com