Generating new objects using existing objects

2

I'm having a hard time doing this.

Assuming I have two objects of type Person , which has the name and job attributes.

  

P1 (name = John, job = Taxi Driver)

     

P2 (name = Mary, job = Programmer)

So I would like to generate the possible combinations of these two objects, of course without attribute errors, for example an object having for example the name Taxiper.

The output I would need in case would be.

  

P3 (name = John, job = Programmer)

     

P4 (name = Maria, job = Taxi Driver)

How can I do this? Are there libraries that allow me?

EDIT

An example 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.

Then when printing this list the output would be:

  

(John, Taxi Driver)

     

(Mary, Developer)

     

(John, Developer)

     

(Maria, Taxi Driver)

    
asked by anonymous 18.08.2015 / 03:49

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 on-the-fly attributes. If the attributes are always the 6 quoted in the comment and the number of objects is small, complete with 3 more 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));
    }
}

Update

I think it's important to mention the complexity of the above algorithm. For three attributes complexity is O (n n) or O the number of attributes.

Anyway, this is a classic example of an algorithm that does not scale well. Depending on the size of n and m it may not be feasible to run it. Anyway, there is not much to do, since you have to generate all combinations.

To learn more about complexity, see this beautiful answer: link

    
18.08.2015 / 04:20