识别具有重复值的键值对
Identify the Key-Value pair having duplicate values
我有一个如下所示的多图:
{20014=[13123], 20013=[45451, 13123]}
键和值在 String
中
如果其他键的值有任何重复,我必须打印那个键值对。在这种情况下,它将是 Key-20013,Value-13123.
如何实现?
我检查了这个 link 但不知道如何获得重复对。
可以这样做:
// Initialize my multimap
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("20014", "13123");
multimap.put("20013", "45451");
multimap.put("20013", "13123");
// Set in which we store the values to know if they exist already
Set<String> allValues = new HashSet<>();
// Convert the multimap into a Map
Map<String, Collection<String>> map = multimap.asMap();
// Iterate over the existing entries
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
String key = entry.getKey();
Collection<String> values = entry.getValue();
// Iterate over the existing values for a given key
for (String value : values) {
// Check if the value has already been defined if so print a log message
if (!allValues.add(value)) {
System.out.println(String.format("Key-%s,Value-%s", key, value));
}
}
}
输出:
Key-20013,Value-13123
您可以反转您的多重地图,并将其视为地图,遍历其条目:
Multimap<String, String> inverse = Multimaps.invertFrom(multimap, HashMultimap.create());
for (Map.Entry<String, Collection<String>> entry : inverse.asMap().entrySet()) {
String value = entry.getKey();
Iterator<String> keysIterator = entry.getValue().iterator();
assert keysIterator.hasNext() : "there is always at least one key";
keysIterator.next(); // skip first key
while (keysIterator.hasNext()) { // each additional key is a duplicate
String key = keysIterator.next();
System.out.println(String.format("Key-%s,Value-%s", key, value));
}
}
输出:
Key-20013,Value-13123
如果您使用的是 ImmutableMultimap
then instead of Multimaps.invertFrom(Multimap, M)
you can simply use ImmutableMultimap.inverse()
:
ImmutableMultimap<String, String> inverse = multimap.inverse();
如果您只是想将重复值映射到它们各自的键,那么您可以使用 Maps.filterValues(Map, Predicate)
:
Map<String, Collection<String>> keysByDuplicatedValue = Maps.filterValues(inverse.asMap(),
keys -> keys.size() > 1);
这将为您提供如下地图:
{13123=[20014, 20013]}
我有一个如下所示的多图:
{20014=[13123], 20013=[45451, 13123]}
键和值在 String
如果其他键的值有任何重复,我必须打印那个键值对。在这种情况下,它将是 Key-20013,Value-13123.
如何实现? 我检查了这个 link 但不知道如何获得重复对。
可以这样做:
// Initialize my multimap
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("20014", "13123");
multimap.put("20013", "45451");
multimap.put("20013", "13123");
// Set in which we store the values to know if they exist already
Set<String> allValues = new HashSet<>();
// Convert the multimap into a Map
Map<String, Collection<String>> map = multimap.asMap();
// Iterate over the existing entries
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
String key = entry.getKey();
Collection<String> values = entry.getValue();
// Iterate over the existing values for a given key
for (String value : values) {
// Check if the value has already been defined if so print a log message
if (!allValues.add(value)) {
System.out.println(String.format("Key-%s,Value-%s", key, value));
}
}
}
输出:
Key-20013,Value-13123
您可以反转您的多重地图,并将其视为地图,遍历其条目:
Multimap<String, String> inverse = Multimaps.invertFrom(multimap, HashMultimap.create());
for (Map.Entry<String, Collection<String>> entry : inverse.asMap().entrySet()) {
String value = entry.getKey();
Iterator<String> keysIterator = entry.getValue().iterator();
assert keysIterator.hasNext() : "there is always at least one key";
keysIterator.next(); // skip first key
while (keysIterator.hasNext()) { // each additional key is a duplicate
String key = keysIterator.next();
System.out.println(String.format("Key-%s,Value-%s", key, value));
}
}
输出:
Key-20013,Value-13123
如果您使用的是 ImmutableMultimap
then instead of Multimaps.invertFrom(Multimap, M)
you can simply use ImmutableMultimap.inverse()
:
ImmutableMultimap<String, String> inverse = multimap.inverse();
如果您只是想将重复值映射到它们各自的键,那么您可以使用 Maps.filterValues(Map, Predicate)
:
Map<String, Collection<String>> keysByDuplicatedValue = Maps.filterValues(inverse.asMap(),
keys -> keys.size() > 1);
这将为您提供如下地图:
{13123=[20014, 20013]}