简体   繁体   English

我应该如何在Java中存储基于两个对象的值?

[英]How should I store a value based on two objects in Java?

I have some objects, lets just call them Person objects. 我有一些对象,让我们称它们为Person对象。 I also have some other objects, lets call them Relationship objects. 我还有一些其他对象,让我们称之为关系对象。

I want to be able to assign a Relationship object between two Person objects, but I'm not really sure what a good way to go about this is. 我希望能够在两个Person对象之间分配一个Relationship对象,但我不确定这是一个好方法。

I was thinking of maybe giving each Person object an ID and then creating a 2D array of Relationship objects using the Person object ID as the key values. 我想的可能是给每个Person对象一个ID,然后使用Person对象ID作为键值创建一个关系对象的2D数组。

I'm not sure if that's the best approach though as the array would have a lot of null values wherever there isn't a relationship between two Person objects. 我不确定这是否是最佳方法,因为在两个Person对象之间没有关系的情况下,数组会有很多空值。

@Makoto's a good idea. @ Makoto是个好主意。 Alternatively, what sounds to me a litte bit more natural is to have Relationship object hold two Person objects passed eg as constructor arguments. 或者,对我来说听起来更自然的是让Relationship对象保存两个Person对象,例如作为构造函数参数传递。 You'll then need to track only the Relationship objects because they'll have knowledge about both Persons that are the parts of it. 然后,您需要仅跟踪关系对象,因为他们将了解作为其中一部分的人员。

class Relationship {
Person firstPerson;
Person secondPerson;

  public Relationship(Person firstPerson, Person secondPerson) {
    this.firstPerson = firstPerson;
    this.secondPerson = secondPerson;
}

Alternatively you can use a public method to pass the reference to the Person objects if you don't want them to be passed via constructor: 或者,如果您不希望它们通过构造函数传递,您可以使用公共方法将引用传递给Person对象:

public void setPersons(Person firstPerson, Person secondPerson) {
  this.firstPerson = firstPerson;
  this.secondPerson = secondPerson;
}

What you describe are three instances: two Person instances and one Relationship instance. 您描述的是三个实例:两个Person实例和一个Relationship实例。

The most straightforward approach is, in the Person class, allow for a Relationship instance to exist, and to wire up the relationships of a Relationship between these two Person s. 最直接的方法是,在Person类中,允许存在Relationship实例,并连接这两个Person之间关系的Relationship

class Person {
    Relationship relationship;

    public void createRelationship(Person person) {
        relationship = new Relationship(this, person);
    }
}

If you wanted the Relationship instance back to keep in a collection or array, you could modify that to simply return a new Relationship as the result of two Person s. 如果您希望将Relationship实例保留在集合或数组中,则可以将其修改为仅返回两个Person的结果的新Relationship

 class Relationship {

    public static Relationship createRelationshipFrom(Person firstPerson, Person secondPerson) {
        return new Relationship(firstPerson, secondPerson);
    }
}

In the natural world, a person can be a part of a relationship, but a relationship can not be part of a person. 在自然界中,一个人可以成为一种关系的一部分,但一种关系不能成为一个人的一部分。 Therefore, having Relationship as a member of Person is undesirable. 因此,具有Relationship作为成员Person是不理想的。 Such a setup prevents a Person from having more than one Relationship :) and also prevents a Person from having no Relationship :( 这样的设置可以防止一个Person拥有多个Relationship :)并且还可以防止一个Person没有Relationship :(

If we can define a Relationship as being between 2 people, and 2 people only, then one possible setup would be to have the Relationship class contain 2 Person members. 如果我们可以将Relationship定义为2人之间,而仅限2人,则可能的设置是让Relationship类包含2个Person成员。 If a Relationship can be between 3 or more people :), then a single List<Person> member may be more helpful. 如果一个Relationship可以在3个或更多人之间:),那么单个List<Person>成员可能会更有帮助。

I want to be able to assign a Relationship object between two Person objects 我希望能够在两个Person对象之间分配一个Relationship对象

If you are trying to discretely ask for dating advice, you have come to the wrong place. 如果你试图离散地询问约会建议,你就来错了地方。

Edit: 编辑:

If you are trying to get a Relationship that any given two people are involved in, create a method like this 如果您想要获得任何给定两个人参与的Relationship ,请创建一个这样的方法

public Relationship find(List<Relationship> relationships, Person person1, Person person2) {
    for (Relationship relationship : relationships) {
        if (relationship.getPerson1().equals(person1) && relationship.getPerson2().equals(person2) {
            return relationship;
        }
        if (relationship.getPerson1().equals(person2) && relationship.getPerson2().equals(person1) {
            return relationship;
        }
    }
}

If performance is a concern, use Set<Relationship> and override the equals() and hashCode() methods of Relationship so that two Relationship objects containing the same people will be considered equal. 如果需要考虑性能,请使用Set<Relationship>并覆盖Set<Relationship>equals()hashCode()方法,以便将包含相同人员的两个Relationship对象视为相等。 Then you can do this 然后你就可以做到这一点

Set<Relationship> relationships = new HashSet<Relationship>();
// populate relationships

Relationship delegate = new Relationship(person1, person2);
int hasRelationship = relationships.contains(delegate);

What you need is an implementation of a Tree in Java, where the nodes are the Person objects, and the edges (the lines that connect two nodes) are the connections. 您需要的是Java中的Tree实现,其中节点是Person对象,边(连接两个节点的线)是连接。 This is the most efficient data model available for the type of storage you need. 这是您所需存储类型的最有效数据模型。

Someone posted a basic tree class structure on SO in the past: Java tree data-structure? 有人在过去在SO上发布了一个基本的树类结构: Java树数据结构?

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

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