Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes HASH MAP & ARRAY LIST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "HASH MAP & ARRAY LIST" Watch "HASH MAP & ARRAY LIST" New topic
Author

HASH MAP & ARRAY LIST

Jigar Thakor
Greenhorn

Joined: Jun 16, 2008
Posts: 2
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


Regards,<br />Jigar Thakor
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This sounds like it might be a JDBC question? I'll move it over there, see if anyone can help.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jigar Thakor
Greenhorn

Joined: Jun 16, 2008
Posts: 2
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Jun 15, 2008
Posts: 11
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
 
wood burning stoves
 
subject: HASH MAP & ARRAY LIST
 
Similar Threads
about hashmap implementation of arraylist
Sorting Maps
Doubt in Hash Map and Hash Table
HashMapping
XML file parsing in Java using SAX