简体   繁体   English

Java社交网络节点集合解决方案

[英]Java social networking node collection solution

Somewhat new to Java. Java有点新鲜。 I have used various Java collections (treeset, hashmap, arraylist) before quite successfully. 在成功之前我使用过各种Java集合(treeset,hashmap,arraylist)。 My problem is similar to a Facebook-like network. 我的问题类似于类似Facebook的网络。 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. 我认为最简单的解决方案是为每个成员动态分配一个新的简单集合,这些成员将链接其他成员名称(现有的或新的),但看起来Java不允许动态分配新集合。

I could have a concatenated string in a hashmap listing all the names associated with the key name, but this seems an akward solution. 我可以在一个hashmap中有一个连接的字符串,列出与键名相关的所有名称,但这似乎是一个不方便的解决方案。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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