What I have is 2 tables :
Users (id, name, lastname)
Friends (id1, id2)
Given these 2 tables, I need to be able to find the distance between 2 users d(id1,id2)
I defined a User class, that holds each of the user attribute in the table.
I need to build a graph. My data structure for the graph is
Map<User, Set<User>>
mapping a user to a set of his friends. How am I building the graph ? I queried the db for the ids of all the users in the db. I got an
int[] userids
Then for each int in this array:
(1) I build a User object, fetching in the db the attributes of that user
(2) I query the Friends table in the db to have the ids of the friends of that user :
int [] friends
(3) for each int in this friends array, I build a User object fetching in the db the attributes of that user and add it to
Set<User> friends = new Set<User>();
Question 1 : Any ideas how to do this better ? It takes forever, given that I have 500 users, and 20000 entries in the Friends table ...
The big problem here is that when 2 users are the "same" in the database, they are referenced in differents objects in my graph !!
Which is messing up my distance algorithm. I start with a User u, get his Friends {f1,f2} , and when I wanna get the friends of friends using graph.get(f1) and graph.get(f2) I get null (for the reason stated in my problems, ie 1 db user in many different User objects)
I need to find a way to build my graph such that 1 given user say (1, John, Doe) is referenced in one and only User object in the heap ...
Question 2 : How ??
Thank you so much