本文介紹了在 Java Map 中查找重復值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想在 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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!