Note that there are some explanatory texts on larger screens.

plurals
  1. POReversing a HashMap <String, Object> to different Sets and/or Lists
    primarykey
    data
    text
    <p>I will try to explain the point that I am stuck as simple as I can.</p> <p>Basically I am trying to implement a solution to handle user data. </p> <p>I have a Map something like <code>&lt;String, Object&gt;</code>, which will store user ids as keys and user objects as values. User object has some fields like location, name, age etc. etc. </p> <p>By using the map, I would like to have some sets or other maps to classify user data. For instance i am trying to get another map like <code>&lt;String, Set&lt;String&gt;&gt;</code> which will store a set of users who lives in the same location and so on. </p> <p>I have tried some tricks as well as checked some links like <a href="http://www.codingforums.com/showthread.php?t=191502" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/7549640/reversing-a-hashmap-from-mapstring-boolean-to-mapboolean-liststring">this</a> and some others, also tried to implement some basic code for reversing </p> <pre><code>Set&lt;String&gt; userid = new HashSet&lt;String&gt;(); Map&lt;String, Set&lt;String&gt;&gt; returnvalue = new HashMap&lt;String, Set&lt;String&gt;&gt;(); HashMap&lt;String, String&gt; userReason = convertForReason(); for (Entry&lt;String, String&gt; entry : userReason.entrySet()) { userid.add(entry.getKey()); } </code></pre> <p>but i am stuck how to create the new map <code>&lt;String. Set&lt;String&gt;&gt;</code>, any ideas how to achieve this? or other solutions for this kind of issues?</p> <p>UPDATE: </p> <p>Well here is what I found for my own question :</p> <p>Map> returnValue = new HashMap>(); HashSet reasonSet = new HashSet();</p> <pre><code> 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&lt;String&gt;(); reasonSet.add(username); returnValue.put(reason, reasonSet); }else{ HashSet&lt;String&gt; tmp = returnValue.get(location); tmp.add(username); returnValue.put(location, tmp); } </code></pre> <p>Thanks to Byter for giving me insight and better idea to solve this :) I am not quite sure how the performance will be affected if the size goes too large though</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload