我會試着解釋一下我堅持儘可能簡單的觀點。將HashMap <String,Object>顛倒爲不同的集合和/或列表
基本上我試圖實現一個解決方案來處理用戶數據。
我有一個類似<String, Object>
的地圖,它將用戶ID作爲鍵和用戶對象存儲爲值。用戶對象有一些字段,如位置,名稱,年齡等。
通過使用地圖,我想有一些集或其他地圖來分類用戶數據。例如,我正試圖獲得另一個地圖,如<String, Set<String>>
,它將存儲一組居住在相同位置的用戶等。
我已經嘗試了一些技巧以及檢查像this和this和其他一些人一些鏈接,也試圖實現一些基本的代碼反轉
Set<String> userid = new HashSet<String>();
Map<String, Set<String>> returnvalue = new HashMap<String, Set<String>>();
HashMap<String, String> userReason = convertForReason();
for (Entry<String, String> entry : userReason.entrySet()) {
userid.add(entry.getKey());
}
但我堅持瞭如何創建新的地圖<String. Set<String>>
,任何想法如何實現這一目標?或其他解決方案的這類問題?
UPDATE:
那麼這裏就是我發現我自己的問題:
地圖> =的returnValue新的HashMap>(); HashSet reasonSet = new HashSet();
for(String userId : userData.keySet()){
UserObject obj = userData.get(userId);
String location = obj.getLocation();
String username = obj.getUserid();
if(returnValue.get(location)==null){
reasonSet = new HashSet<String>();
reasonSet.add(username);
returnValue.put(reason, reasonSet);
}else{
HashSet<String> tmp = returnValue.get(location);
tmp.add(username);
returnValue.put(location, tmp);
}
感謝Byter給我的見解和更好的主意來解決這個:)我不太知道如何表現將受到影響,如果大小會太大而