我想在 HashMap 中显示值.HashMap 可能有重复的值(但不是重复的键),但我只想显示一次值.
I want to display the values in a HashMap. A HashMap may have duplicate values (but not duplicate keys), but I want to display a value only once.
所以我应该找出 Map 是否有重复值.我知道我们可以遍历 Map 并使用 map.containsValue(value) 的返回布尔值.我想知道是否存在任何方法来查找地图中的重复值,或者我们应该自己编写代码?
So I should find whether the Map has duplicate values. I know we can iterate over the Map and use the return boolean of map.containsValue(value). I want to know whether any method exists to find duplicate values in map or we should I write code myself?
一个简单的解决方案是将值列表的大小与值集进行比较.
A simple solution would be to compare the size of your values list with your values set.
// pseudo-code
List<T> valuesList = map.values();
Set<T> valuesSet = new HashSet<T>(map.values);
// check size of both collections; if unequal, you have duplicates
这篇关于在 Java Map 中查找重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
使用 Java-8 Streams API 将字符串列表转换为 MapConvert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 将字符串列表转换为 Map)
从 JSON 获取数据Getting data from JSON(从 JSON 获取数据)
javalinkedhashmap迭代java linkedhashmap iteration(javalinkedhashmap迭代)
将对象列表转换为 MapConverting a list of objects to Map(将对象列表转换为 Map)
用一个固定的Key对应一个HashSet创建一个HashMap.出Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一个固定的Key对应一个HashSet创建一个HashMap.出发点)
HttpMessageConverter 异常:RestClientException:无法写入请HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 异常:RestC