简体   繁体   中英

Java social networking node collection solution

Somewhat new to Java. I have used various Java collections (treeset, hashmap, arraylist) before quite successfully. My problem is similar to a Facebook-like network. I have various users in a membership organization and I want to store in a collection for each individual in our membership other members who are linked to this 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 (existing or new) linked, but it appears Java does not allow dynamic allocation of new collections.

I could have a concatenated string in a hashmap listing all the names associated with the key name, but this seems an akward solution. I assume this is a social common network-like problem that has an elegant solution. Suggestions?

Why don't you model it like 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 encounter 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM