How to get data from a list only when there are two equal codes

5

Currently I can only get the different data from two lists, for example:

Lista X
Código 1
Código 2

Lista z:
Código 1
Código 3

I can only get the code 2 and 3 . Now the need arises to get the same data, following the above example I want to get only the code 1 since it exists in both lists.

This is the code I use to get the different elements of two lists, and then I populate a table with this data:

List<Contratos> listaContratos = listarBanco();

        Cadastros cadastro = new Cadastros();
        ConsultaCadastro consultaCadastro = new ConsultaCadastro();
        List<Cadastros> listaCadastros;
        try {
            cadastro.setContratante(pesquisar.getText());
            cadastro.setCodigoContrato(pesquisar.getText());
            //Preenche uma lista de Cadastros
            listaCadastros = consultaCadastro.buscar(cadastro);

            Iterator<Cadastros> iter = listaCadastros.iterator();
            while (iter.hasNext()) {
                Cadastros solicitacao = iter.next();
                for (Contratos s : listaContratos) {
                    Long t = s.getCodigoContrato();
                    if (t == Long.parseLong(solicitacao.getCodigoContrato())) {
                        iter.remove();
                        System.out.println("Remove");
                    }
                }
            }

            tblCadastros.setItems(FXCollections.observableArrayList(listaCadastros));

How do I get just the same, or if it is easier as I pass a list of parameters to make an appointment in the bank, these two solutions would solve my problem.

    
asked by anonymous 23.06.2015 / 21:24

1 answer

8

In the Set collection there is a method that does just that, its name is .retainAll() .

Example to use:

List<Integer> list1 = new ArrayList();
list1.add(1);
list1.add(2);

List<Integer> list2 = new ArrayList();
list2.add(1);
list2.add(3);

Set<Integer> intersecao = new HashSet(list1);
intersecao.retainAll(list2);

System.out.println(intersecao);
  

[1]


An addendum, if you are going to work with a class that has several attributes and you want to look only for% of equal%, you can override your código method, eg:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final SuaClasse other = (SuaClasse) obj;
    if (this.codigo != other.codigo) {
        return false;
    }
    return true;
}

If you want to create a .equals() to compare to another class, it needs to look something like this:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass() && obj.getClass() != OutraClasse.class) {
        return false;
    }
    if (getClass() == obj.getClass()) {
        final SuaClasse other = (SuaClasse) obj;
        if (this.codigo != other.codigo) {
            return false;
        }
    } else {
        final OutraClasse other = (OutraClasse) obj;
        if (this.codigo != other.codigo) {
            return false;
        }
    }
    return true;
}

That way it can have 20 different attributes, but if the code is equal, it returns true and works with equals .


Putting into practice

class SuaClasse {

    private int codigo;
    private String nome;

    // get/set

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass() && obj.getClass() != OutraClasse.class) {
            return false;
        }
        if (getClass() == obj.getClass()) {
            final SuaClasse other = (SuaClasse) obj;
            if (this.codigo != other.codigo) {
                return false;
            }
        } else {
            final OutraClasse other = (OutraClasse) obj;
            if (this.codigo != other.codigo) {
                return false;
            }
        }
        return true;
    }
}

And again at the intersection:

List<SuaClasse> list1 = new ArrayList();
list1.add(new SuaClasse(1, "maicon"));
list1.add(new SuaClasse(2, "maicon"));

List<SuaClasse> list2 = new ArrayList();
list2.add(new OutraClasse(1, "techies"));
list2.add(new OutraClasse(3, "techies"));

Set<SuaClasse> intersecao = new HashSet(list1); // pega o equals da SuaClasse
intersecao.retainAll(list2);

System.out.println("Tamanho: " + intersecao.size());
for (SuaClasse elemento : intersecao) {
    System.out.println(elemento.getCodigo() + " - " + elemento.getNome());
}
  

Size: 1

     

1 - maicon


Important!

This change in .retainAll() affects all methods you compare to objects, so if you use some .equals() or even .indexOf() what prevails is your equals , that is, just by comparing the code .

    
23.06.2015 / 21:43