• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HASH MAP & ARRAY LIST

 
Jigar Thakor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to return HashMap which contains unique key (here server_id), as well list of users(usr_ids).

I am getting single record at a time which is giving me, server_id as well as user_id.( and have hundreds of rows..)

Now I need to filter only Unique Server_Ids and corresponding user_IDs, and put it into Hash Map.

Regards,
Jigar A. Thakor
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like it might be a JDBC question? I'll move it over there, see if anyone can help.
 
Jigar Thakor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I am getting the data as I posted earlier.

This is not JDBC or query related question.

I want to achieve groupism of user_ids by server_ids.
key = server_id
values stored in list = user_id.

Please post it on the General Java - Difficult
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to iteratate through your collection of data adding an entry keyed on server id to a map where the value is the user id? Sounds easy enough; what is the issue you are having? Are you unsure which classes to use? Or are you looking for help with the logic?
 
Paul Sisco
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JIGAR THAKOR:
Here I am getting the data as I posted earlier.

This is not JDBC or query related question.

I want to achieve groupism of user_ids by server_ids.
key = server_id
values stored in list = user_id.

Please post it on the General Java - Difficult



I'm not completely sure which problem you have, but I'll take a shot anyway.

It sounds like your hashmap looks like this?
HashMap<String, ArrayList> idMap = new HashMap<String, ArrayList>();

This would give you a hashmap of unique server_id's where each server_id key has 1 ArrayList associated with it. That ArrayList would be the last one added to the HashMap.

If your database is giving you the same server_id multiple times and each time it can have a different list of user_id's then you could do something like this:

String newServerId = ...
ArrayList newUserList = ...
ArrayList userList = (ArrayList)idMap.get(newServerId);
if(userList == null){
//add the newServerId and newUserList to the hashMap
}else{
//add the newUserlist to the userList you got from the hashmap.
}


This could certainly give you duplicate user_id's in the arrayLists. If that is a problem, try using a Set instead of an ArrayList for the user_id's in the idMap.

Set<String> userIdSet = new HashSet<String>();
HashMap<String, Set<String>> idMap = new HashMap<String, Set<String>>();


I hope this helps.

Paul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic