我有一个如下所示的多图:
{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
关于java - 识别具有重复值的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38850988/