How to store a value based on two objects in Java?

I have some objects, let's just call them Person objects. I also have some other objects, let's call them relationship objects.

I want to be able to assign a Relationship object between two Person objects, but I'm not entirely sure what a good way to do this.

I was thinking about what I can give each Person object an ID and then create a 2D array of Relationship objects using the ID of the Person object as the key values.

I'm not sure if this is the best approach as the array will have many null values ​​wherever there is no connection between the two Person objects.

+3


source to share


4 answers


@Makoto is a good idea. Alternatively, what seems more natural to me is that the Relationship object has two Person objects passed in for example. as constructor arguments. Then you only need to keep track of the "Relationship" objects, because they will know about all the Persons that are 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 a reference to Person objects if you don't want them to pass through the constructor:

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

      

+2


source


What you are describing is three instances: two instances Person

and one instance Relationship

.

The simplest approach is for a class Person

to have an instance Relationship

and to bind the relationship a Relationship

between the two Person

s.

class Person {
    Relationship relationship;

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

      



If you want an instance to Relationship

return to a collection or array, you can change that to just return a new one Relationship

as a result of two Person

s.

 class Relationship {

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

      

+1


source


In the natural world, a person can be part of a relationship, but a relationship cannot be part of a person. Hence, having Relationship

as a member is Person

undesirable. This setting prevents Person

more than one Relationship

:) and also prevents missing Person

Relationship

:(

If we can define Relationship

as 2 to 2 people, then one possible setting would be for the class to Relationship

contain 2 Person

members. If a Relationship

can be between 3 or more people :), then one member may be more useful List<Person>

.

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

If you're trying to discreetly ask for dating advice, you're in the wrong place.

Edit:

If you are trying to get Relationship

that all two people are associated with, 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 an issue, use Set<Relationship>

and override the relationship methods equals()

and hashCode()

so that two objects Relationship

containing the same people are 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);

      

0


source


You need a Tree

Java implementation where the nodes are Person objects and the edges (the lines connecting two nodes) are junctions. This is the most efficient data model available for the type of storage required.

Someone posted the basic structure of tree classes in SO in the past: Java tree data structure?

0


source







All Articles