user11406
user11406

Reputation: 1258

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

I have some objects, lets just call them Person objects. 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.

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.

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.

Upvotes: 3

Views: 2285

Answers (4)

ttarczynski
ttarczynski

Reputation: 1014

@Makoto's a good idea. Alternatively, what sounds to me a litte bit more natural is to have Relationship object hold two Person objects passed e.g. as constructor arguments. 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:

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

Upvotes: 2

pathfinderelite
pathfinderelite

Reputation: 3147

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. Such a setup prevents a Person from having more than one Relationship :) and also prevents a Person from having no 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. If a Relationship can be between 3 or more people :), then a single List<Person> member may be more helpful.

I want to be able to assign a Relationship object between two Person objects

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

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. Then you can do this

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

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

Upvotes: 0

mittelmania
mittelmania

Reputation: 3571

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. 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?

Upvotes: 0

Makoto
Makoto

Reputation: 106528

What you describe are three instances: two Person instances and one Relationship instance.

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 Persons.

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 Persons.

 class Relationship {

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

Upvotes: 1

Related Questions