Generating new objects using existing objects

Asked

Viewed 121 times

2

I’m having a hard time doing this.

Supposing I have two objects of the kind Person, which has the attributes name and employment.

P1 (name=John, job=Taxi driver)

P2 (name=Maria, job=Programmer)

Then I would like to generate the possible combinations of these two objects, of course without attributes errors, for example an object having for example the name Taxi driver.

The way out I would need in case.

P3 (name=John, job=Programmer)

P4 (name=Maria, job=Taxi driver)

How can I do that? There are libraries that allow me?

EDIT

An example of code would look something like this:

...

Pessoa p1 = new Pessoa("João","Taxista");
Pessoa p2 = new Pessoa("Maria", Programador;

List<Pessoa> listaDePessoas = new ArrayList<Pessoa>;
listaDePessoas.add(p1);
listaDePessoas.add(p2);

List<Pessoa> novaLista = new ArrayList<Pessoa>;

novaLista = geraCombinacoes(listaDePessoas); //esse seria o método por exemplo.

Ai when printing that list the output would be:

(João, Taxista)

(Maria, Programador)

(João, Programador)

(Maria, Taxista)

  • I couldn’t figure out what you’re looking for. What combinations are you talking about? Put some code showing where you’re going.

  • I don’t quite understand your doubt... as far as I can tell, you need an object like Pessoa and an object of the type Emprego, where the Person could be instantiated with different names (John, Mary, etc.) and could be related to some Employment (Programmer, Taxi driver, etc.).

  • Hello @bigown, explaining better, let’s assume that I have a list with 2 objects that are instances of my person class, in case they are part of this list P1 and P2 objects. I want to pass this list as a parameter for a method, and it returns me the possible combinations, which I mentioned above, the combination would be between the attributes of the objects to create new objects. I’ll put it as code, see if it gets better.

  • It’s a simple combination algorithm, right? You want everyone to have all the trades?

  • This @Cantoni, I want everyone to have all the professions, and other tmb attributes, if there are other attributes, they also enter the combination.

  • Right. I made an algorithm here with just these two attributes. It’s so simple that I don’t think that’s what you want. It is a quadratic algorithm (one is inside the other). If you want me to post. The version for arbitrary attributes needs to use Reflection.

  • @Cantoni, can post, any help is valid, anything I try to develop more, is already a start! But really, where I will actually use I have a 6 attributes. I put only 2 to facilitate the explanation.

  • I put a code to illustrate better. I don’t know if I understand better. In this case, all people have to have all the professions. In case you had an attribute called: address. Every person would have to have every job and every address, in this case, every possible combination.

  • Do you want something generic or will always be these 6 attributes?

  • For now it will be only those six attributes, but in the future it may have to change. Something generic would be great, but if only the 6 attributes remain, it’s still great!

Show 5 more comments

1 answer

3


The algorithm below works for three attributes. For an arbitrary amount a more elegant solution is to use Reflection in order to discover these attributes on-the-fly. If the attributes are always the 6 cited in the comment and the number of objects is small, complete with 3 more is nested and solve the problem, whatever it is.

class Pessoa {

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

    String nome;
    String emprego;
    String endereco;

    public String toString() {
        return nome +  " - " + emprego + " - " + endereco;
    }
}


public static void main(String[] args) {
    List<Pessoa> pessoas = new ArrayList<Pessoa>();

    pessoas.add(new Pessoa("Joao","Programador","ACD"));
    pessoas.add(new Pessoa("Jose","Analista","DFSD"));
    pessoas.add(new Pessoa("Maria","Gerente","ASEW"));
    pessoas.add(new Pessoa("Pedro","Tester","VCVC"));

    List<Pessoa> resultado = new ArrayList<Pessoa>();

    for(int i = 0; i < pessoas.size(); i++)
        for(int j = 0; j < pessoas.size(); j++)
            for(int k = 0; k < pessoas.size(); k++)
                resultado.add(new Pessoa(pessoas.get(i).nome, pessoas.get(j).emprego, pessoas.get(k).endereco));                              

    for(int i = 0; i < resultado.size(); i++) {
        System.out.println(resultado.get(i));
    }
}

Updating

I think it is important to mention the complexity of the above algorithm. For three attributes complexity is O(n * n * n) or O(n3). Each acidified attribute will add one more loop and thus add + 1 to the exponent. Thus, with 6 attributes the complexity is O(n6).

Generalizing, the complexity is O(nm), where n is the number of people and m the number of attributes.

Anyway, this is a classic example of an algorithm that doesn’t scale well. Depending on the size of n and m it can become impossible to run it. Anyway, there is not much to do, since it is necessary to generate all combinations.

to learn more about complexity see this beautiful answer: /a/56868/3084

  • I am on mobile now @Cantoni, tomorrow when I test I will give a reply. Thank you so much for the layout. Thank you!

Browser other questions tagged

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