本文整理了Java中com.google.common.collect.Maps.difference()
方法的一些代码示例,展示了Maps.difference()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Maps.difference()
方法的具体详情如下:
包路径:com.google.common.collect.Maps
类名称:Maps
方法名:difference
[英]Computes the difference between two maps. This difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.
Since this method uses HashMap instances internally, the keys of the supplied maps must be well-behaved with respect to Object#equals and Object#hashCode.
Note:If you only need to know whether two maps have the same mappings, call left.equals(right) instead of this method.
[中]计算两个贴图之间的差异。此差异是调用此方法时映射状态的不可变快照。它永远不会改变,即使以后地图会改变。
由于此方法在内部使用HashMap实例,因此提供的映射的键必须在Object#equals和Object#hashCode方面表现良好。
注意:如果您只需要知道两个映射是否具有相同的映射,请调用left。等于(右)而不是此方法。
代码示例来源:origin: springside/springside4
/**
* 对两个Map进行比较,返回MapDifference,然后各种妙用.
*
* 包括key的差集,key的交集,以及key相同但value不同的元素。
*
* @see com.google.common.collect.MapDifference
*/
public static <K, V> MapDifference<K, V> difference(Map<? extends K, ? extends V> left,
Map<? extends K, ? extends V> right) {
return Maps.difference(left, right);
}
代码示例来源:origin: vipshop/vjtools
/**
* 对两个Map进行比较,返回MapDifference,然后各种妙用.
*
* 包括key的差集,key的交集,以及key相同但value不同的元素。
*
* @see com.google.common.collect.MapDifference
*/
public static <K, V> MapDifference<K, V> difference(Map<? extends K, ? extends V> left,
Map<? extends K, ? extends V> right) {
return Maps.difference(left, right);
}
代码示例来源:origin: google/guava
/**
* Computes the difference between two maps. This difference is an immutable snapshot of the state
* of the maps at the time this method is called. It will never change, even if the maps change at
* a later time.
*
* <p>Since this method uses {@code HashMap} instances internally, the keys of the supplied maps
* must be well-behaved with respect to {@link Object#equals} and {@link Object#hashCode}.
*
* <p><b>Note:</b>If you only need to know whether two maps have the same mappings, call {@code
* left.equals(right)} instead of this method.
*
* @param left the map to treat as the "left" map for purposes of comparison
* @param right the map to treat as the "right" map for purposes of comparison
* @return the difference between the two maps
*/
@SuppressWarnings("unchecked")
public static <K, V> MapDifference<K, V> difference(
Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
if (left instanceof SortedMap) {
SortedMap<K, ? extends V> sortedLeft = (SortedMap<K, ? extends V>) left;
return difference(sortedLeft, right);
}
return difference(left, right, Equivalence.equals());
}
代码示例来源:origin: apache/hive
/**
* Helper method to compare 2 Map instances, for equivalence.
* @param lhs First map to be compared.
* @param rhs Second map to be compared.
* @return true, if the 2 Maps contain the same entries.
*/
private static boolean equivalent(Map<String, String> lhs, Map<String, String> rhs) {
return lhs.size() == rhs.size() && Maps.difference(lhs, rhs).areEqual();
}
代码示例来源:origin: apache/incubator-druid
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
EventHolder that = (EventHolder) o;
if (offset != that.offset) {
return false;
}
if (!Maps.difference(event, ((EventHolder) o).event).areEqual()) {
return false;
}
if (segmentId != null ? !segmentId.equals(that.segmentId) : that.segmentId != null) {
return false;
}
return true;
}
代码示例来源:origin: google/guava
public void testMapDifferenceOfSortedMapIsSorted() {
Map<Integer, Integer> map = SORTED_SINGLETON;
MapDifference<Integer, Integer> difference = Maps.difference(map, EMPTY);
assertTrue(difference instanceof SortedMapDifference);
}
代码示例来源:origin: google/guava
public void testMapDifferenceEquals() {
Map<Integer, String> left = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e");
Map<Integer, String> right = ImmutableMap.of(1, "a", 3, "f", 5, "g", 6, "z");
Map<Integer, String> right2 = ImmutableMap.of(1, "a", 3, "h", 5, "g", 6, "z");
MapDifference<Integer, String> original = Maps.difference(left, right);
MapDifference<Integer, String> same = Maps.difference(left, right);
MapDifference<Integer, String> reverse = Maps.difference(right, left);
MapDifference<Integer, String> diff2 = Maps.difference(left, right2);
new EqualsTester()
.addEqualityGroup(original, same)
.addEqualityGroup(reverse)
.addEqualityGroup(diff2)
.testEquals();
}
代码示例来源:origin: google/guava
public void testSortedMapDifferenceEquals() {
SortedMap<Integer, String> left = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e");
SortedMap<Integer, String> right = ImmutableSortedMap.of(1, "a", 3, "f", 5, "g", 6, "z");
SortedMap<Integer, String> right2 = ImmutableSortedMap.of(1, "a", 3, "h", 5, "g", 6, "z");
SortedMapDifference<Integer, String> original = Maps.difference(left, right);
SortedMapDifference<Integer, String> same = Maps.difference(left, right);
SortedMapDifference<Integer, String> reverse = Maps.difference(right, left);
SortedMapDifference<Integer, String> diff2 = Maps.difference(left, right2);
new EqualsTester()
.addEqualityGroup(original, same)
.addEqualityGroup(reverse)
.addEqualityGroup(diff2)
.testEquals();
}
代码示例来源:origin: ctripcorp/apollo
boolean isModified(Namespace namespace) {
Release release = releaseService.findLatestActiveRelease(namespace);
List<Item> items = itemService.findItemsWithoutOrdered(namespace.getId());
if (release == null) {
return hasNormalItems(items);
}
Map<String, String> releasedConfiguration = gson.fromJson(release.getConfigurations(), GsonType.CONFIG);
Map<String, String> configurationFromItems = generateConfigurationFromItems(namespace, items);
MapDifference<String, String> difference = Maps.difference(releasedConfiguration, configurationFromItems);
return !difference.areEqual();
}
代码示例来源:origin: SonarSource/sonarqube
private void compareActivationParams(DbSession session, ActiveRuleDto leftRule, ActiveRuleDto rightRule, QProfileComparisonResult result) {
RuleKey key = leftRule.getRuleKey();
Map<String, String> leftParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleId(session, leftRule.getId()));
Map<String, String> rightParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleId(session, rightRule.getId()));
if (leftParams.equals(rightParams) && leftRule.getSeverityString().equals(rightRule.getSeverityString())) {
result.same.put(key, leftRule);
} else {
ActiveRuleDiff diff = new ActiveRuleDiff();
diff.leftSeverity = leftRule.getSeverityString();
diff.rightSeverity = rightRule.getSeverityString();
diff.paramDifference = Maps.difference(leftParams, rightParams);
result.modified.put(key, diff);
}
}
代码示例来源:origin: google/guava
Maps.newTreeMap(ImmutableSortedMap.of(1, "a", 3, "f", 5, "g", 6, "z"));
SortedMapDifference<Integer, String> diff1 = Maps.difference(left, right);
left.put(6, "z");
assertFalse(diff1.areEqual());
代码示例来源:origin: google/guava
SortedMapDifference<Integer, String> diff1 = Maps.difference(left, right);
assertFalse(diff1.areEqual());
assertThat(diff1.entriesOnlyOnLeft().entrySet())
diff1.toString());
SortedMapDifference<Integer, String> diff2 = Maps.difference(right, left);
assertFalse(diff2.areEqual());
assertThat(diff2.entriesOnlyOnLeft().entrySet()).contains(Maps.immutableEntry(6, "z"));
代码示例来源:origin: google/guava
public void testMapDifferenceTypical() {
Map<Integer, String> left = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e");
Map<Integer, String> right = ImmutableMap.of(1, "a", 3, "f", 5, "g", 6, "z");
MapDifference<Integer, String> diff1 = Maps.difference(left, right);
assertFalse(diff1.areEqual());
assertEquals(ImmutableMap.of(2, "b", 4, "d"), diff1.entriesOnlyOnLeft());
assertEquals(ImmutableMap.of(6, "z"), diff1.entriesOnlyOnRight());
assertEquals(ImmutableMap.of(1, "a"), diff1.entriesInCommon());
assertEquals(
ImmutableMap.of(
3, ValueDifferenceImpl.create("c", "f"), 5, ValueDifferenceImpl.create("e", "g")),
diff1.entriesDiffering());
assertEquals(
"not equal: only on left={2=b, 4=d}: only on right={6=z}: "
+ "value differences={3=(c, f), 5=(e, g)}",
diff1.toString());
MapDifference<Integer, String> diff2 = Maps.difference(right, left);
assertFalse(diff2.areEqual());
assertEquals(ImmutableMap.of(6, "z"), diff2.entriesOnlyOnLeft());
assertEquals(ImmutableMap.of(2, "b", 4, "d"), diff2.entriesOnlyOnRight());
assertEquals(ImmutableMap.of(1, "a"), diff2.entriesInCommon());
assertEquals(
ImmutableMap.of(
3, ValueDifferenceImpl.create("f", "c"), 5, ValueDifferenceImpl.create("g", "e")),
diff2.entriesDiffering());
assertEquals(
"not equal: only on left={6=z}: only on right={2=b, 4=d}: "
+ "value differences={3=(f, c), 5=(g, e)}",
diff2.toString());
}
代码示例来源:origin: google/guava
MapDifference<Integer, String> diff1 = Maps.difference(left, right, caseInsensitiveEquivalence);
assertFalse(diff1.areEqual());
assertEquals(ImmutableMap.of(2, "b", 4, "d"), diff1.entriesOnlyOnLeft());
diff1.toString());
MapDifference<Integer, String> diff2 = Maps.difference(right, left, caseInsensitiveEquivalence);
assertFalse(diff2.areEqual());
assertEquals(ImmutableMap.of(6, "Z"), diff2.entriesOnlyOnLeft());
代码示例来源:origin: google/guava
public void testMapDifferenceEmptySingleton() {
MapDifference<Integer, Integer> diff = Maps.difference(EMPTY, SINGLETON);
assertFalse(diff.areEqual());
assertEquals(EMPTY, diff.entriesOnlyOnLeft());
assertEquals(SINGLETON, diff.entriesOnlyOnRight());
assertEquals(EMPTY, diff.entriesInCommon());
assertEquals(EMPTY, diff.entriesDiffering());
assertEquals("not equal: only on right={1=2}", diff.toString());
}
代码示例来源:origin: google/guava
public void testMapDifferenceSingletonEmpty() {
MapDifference<Integer, Integer> diff = Maps.difference(SINGLETON, EMPTY);
assertFalse(diff.areEqual());
assertEquals(SINGLETON, diff.entriesOnlyOnLeft());
assertEquals(EMPTY, diff.entriesOnlyOnRight());
assertEquals(EMPTY, diff.entriesInCommon());
assertEquals(EMPTY, diff.entriesDiffering());
assertEquals("not equal: only on left={1=2}", diff.toString());
}
代码示例来源:origin: google/guava
public void testSortedMapDifferenceEmptySingleton() {
SortedMapDifference<Integer, Integer> diff = Maps.difference(SORTED_EMPTY, SORTED_SINGLETON);
assertFalse(diff.areEqual());
assertEquals(SORTED_EMPTY, diff.entriesOnlyOnLeft());
assertEquals(SORTED_SINGLETON, diff.entriesOnlyOnRight());
assertEquals(SORTED_EMPTY, diff.entriesInCommon());
assertEquals(SORTED_EMPTY, diff.entriesDiffering());
assertEquals("not equal: only on right={1=2}", diff.toString());
}
代码示例来源:origin: google/guava
public void testSortedMapDifferenceSingletonEmpty() {
SortedMapDifference<Integer, Integer> diff = Maps.difference(SORTED_SINGLETON, SORTED_EMPTY);
assertFalse(diff.areEqual());
assertEquals(SORTED_SINGLETON, diff.entriesOnlyOnLeft());
assertEquals(SORTED_EMPTY, diff.entriesOnlyOnRight());
assertEquals(SORTED_EMPTY, diff.entriesInCommon());
assertEquals(SORTED_EMPTY, diff.entriesDiffering());
assertEquals("not equal: only on left={1=2}", diff.toString());
}
代码示例来源:origin: google/guava
public void testMapDifferenceEmptyEmpty() {
MapDifference<Integer, Integer> diff = Maps.difference(EMPTY, EMPTY);
assertTrue(diff.areEqual());
assertEquals(EMPTY, diff.entriesOnlyOnLeft());
assertEquals(EMPTY, diff.entriesOnlyOnRight());
assertEquals(EMPTY, diff.entriesInCommon());
assertEquals(EMPTY, diff.entriesDiffering());
assertEquals("equal", diff.toString());
}
代码示例来源:origin: google/guava
public void testSortedMapDifferenceEmptyEmpty() {
SortedMapDifference<Integer, Integer> diff = Maps.difference(SORTED_EMPTY, SORTED_EMPTY);
assertTrue(diff.areEqual());
assertEquals(SORTED_EMPTY, diff.entriesOnlyOnLeft());
assertEquals(SORTED_EMPTY, diff.entriesOnlyOnRight());
assertEquals(SORTED_EMPTY, diff.entriesInCommon());
assertEquals(SORTED_EMPTY, diff.entriesDiffering());
assertEquals("equal", diff.toString());
}
内容来源于网络,如有侵权,请联系作者删除!