Java social networking solution node

Several new to Java. I've used various Java collections (treeet, hashmap, arraylist) quite often. My problem is similar to Facebook-like network. I have different users in a member, and I want to store in a collection for each person in our membership other members that are related to that member by interest. I thought the simplest solution would be to dynamically allocate a new simple collection by name for each member that would have other member names associated (existing or new), but it looks like Java does not allow dynamic allocation of new collections.

I could have a concatenated string in a hashmap that lists all the names associated with the key name, but that looks like akward's solution. I am guessing this is a social shared networking problem that has an elegant solution. Suggestions?

+3


source to share


1 answer


Why don't you model it as a graph?

class Node {
    private String name;
    // TODO: Write your getters / setters.
}

class Edge {
    private Edge source, destination;
    // TODO: Write your getters / setters.
}

List<Node> nodes = new ArrayList<Node>();
List<Edge> edges = new ArrayList<Edge>();

      



Then, if you come across a relationship, you can do the following:

Node alice = new Node("Alice Kentucky");
if (!nodes.contains(alice)) { nodes.add(alice); }
edges.add(new Edge(bob, alice)); // where Bob is already in the node list

      

0


source







All Articles