How do we know which object another object belongs to?

Asked

Viewed 59 times

0

I’ve tried to come up with a simpler example, so I have a person who has a list of cars. If I have a C1 car, how will I know who it belongs to?

    public class Pessoa {

        private String nome;
        private ListaCarros carros;

        public Pessoa(String nome, ListaCarros carros) {
            this.nome = nome;
            this.carros = carros;
        }
    }

    public class ListaCarros {

        private HashMap<String, Carro> listaCarros;

        public ListaCarros() {
            listaCarros = new HashMap<>();
        }

        public void adicionar(Carro carro) {
            listaCarros.put(carro.getMatricula(), carro);
        } 

    }

    public class Carro {

        private String matricula;

        public Carro(String matricula) {
            this.matricula = matricula;
        }  

        public String getMatricula() {
            return matricula;
        }

    }
    public class Teste{
        public static void main(String[] args){

            Carro c1 = new Carro("00-AA-00");
            Carro c2 = new Carro("99-AA-99");

            ListaCarros l1 = new ListaCarros();
            l1.adicionar(c2);
            l1.adicionar(c1);

            Pessoa p1 = new Pessoa("Afonso", l1);

            //Como poderei fazer isto?
            //c1.getPessoa().getNome(); 


        }
    }

How can I get the owner’s name?

The only solution will be to put one more parameter with the name of the parent?

  • 1

    Somehow you would have to have a reference to the Person inside the Car object, for him to know who the "father" is. There is no built-in mechanism in language to know who holds a reference to an object, and even if there was, there could be more than one "father", there could be a reference in a local variable, etc. I also suggest abolishing the List Car class and incorporating its functionality into the Person class, so that the method itself adds() already assigns Person to the Car.

1 answer

2


There are several ways to solve your problem:

Minimal modification

For this possibility I tried to modify the minimum of your code so that you can understand better. You should link to Pessoa at the Carro:

  1. Pessoa should have only the name as property.
  2. ListaCarros when adding, need the parameter Pessoa. And the HashMap is of <Pessoa, List<Carro>>.

Code:

class Pessoa {
    private String nome;

    public Pessoa(String nome) {
        this.nome = nome;
    }

    public String getNome() {
        return nome;
    }
}

class ListaCarros {
    private Map<Pessoa, List<Carro>> listaCarros = new HashMap<>();

    public void adicionar(Pessoa pessoa, Carro carro) {
        if (listaCarros.containsKey(pessoa)) {
            List<Carro> list = listaCarros.get(pessoa);
            list.add(carro);
            listaCarros.put(pessoa, list);
        } else {
            List<Carro> carros = new ArrayList<>();
            carros.add(carro);
            listaCarros.put(pessoa, carros);
        }
    }

    public Pessoa getPessoa(Carro carro) {
        for (Map.Entry<Pessoa, List<Carro>> entry : listaCarros.entrySet()) {
            Pessoa key = entry.getKey();
            List<Carro> value = entry.getValue();
            if (value.contains(carro)) {
                return key;
            }
        }
        return null;
    }
}

class Carro {

    private String matricula;

    public Carro(String matricula) {
        this.matricula = matricula;
    }

    public String getMatricula() {
        return matricula;
    }

}

public class Teste {
    public static void main(String[] args) {
        Carro c1 = new Carro("00-AA-00");
        Carro c2 = new Carro("99-AA-99");
        Pessoa p1 = new Pessoa("Afonso");

        ListaCarros l1 = new ListaCarros();
        l1.adicionar(p1, c2);
        l1.adicionar(p1, c1);

        System.out.println(l1.getPessoa(c1).getNome());
    }
}

NOTE: Code may be simpler and more compact, but I left it so you understand what I did.

Other possibilities

To make your code better, some scenarios:

  1. Remove the use of class ListaCarros
  2. Analyze what is most important in your code: the Carro or the Pessoa.
    • If it is Carro, make for each Carro a list of Pessoa
    • If it is Pessoa, make for each Pessoa a list of Carro.
  3. Depending on your object, take into account that you will have to implement the two methods below to search in the list:
    public int hashCode();
    public boolean equals(Object o);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.