0

I will try to explain the point that I am stuck as simple as I can.

Basically I am trying to implement a solution to handle user data.

I have a Map something like <String, Object>, which will store user ids as keys and user objects as values. User object has some fields like location, name, age etc. etc.

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 <String, Set<String>> which will store a set of users who lives in the same location and so on.

I have tried some tricks as well as checked some links like this and this and some others, also tried to implement some basic code for reversing

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());
}

but i am stuck how to create the new map <String. Set<String>>, any ideas how to achieve this? or other solutions for this kind of issues?

UPDATE:

Well here is what I found for my own question :

Map> returnValue = new 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);
        }

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

Community
  • 1
  • 1
denizdurmus
  • 1,289
  • 1
  • 13
  • 39

1 Answers1

0

Suppose you have location as userdata and you want different users of given location...

 Map<String, Set<String>> returnvalue = new HashMap<String, Set<String>>();
    Map<String, UserObject> userData = getUserData();

    for (String userId : userData.keySet()) {
        UserObject obj = userData.get(userId);
        String location = obj.getLocation();
        Set<String> usersInGivenLocation = returnvalue.get(location);
        if(usersInGivenLocation == null) {
           usersInGivenLocation = new HashSet<String>();
           returnvalue.put(userId,usersInGivenLocation);
        }
        usersInGivenLocation.add(userId);
    }

I Hope this is what u want...

Byter
  • 1,132
  • 1
  • 7
  • 12