如何迭代两个多图并打印文件中的差异?
How to iterate two multi maps and print the difference in file?
我有两个多图如下:
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]
并且,如果您想包含任一地图的唯一键的差异,请将 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]
我有两个多图如下:
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]
并且,如果您想包含任一地图的唯一键的差异,请将 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]