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

Asked

Viewed 1,046 times

5

Currently I can only take 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 arose the need to take the same data, following the example from above I want to take only the code 1 because it exists on both lists.

This is the code I use to take the different elements from two lists, and then fill 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));

Since I can only get the same ones, or if it’s easier as a list of parameters to make a query in the bank, these two solutions would solve my problem.

1 answer

8


In the collection Set there is a method that does just that, his 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 search only for the código equal, you can override your method .equals(), example:

@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 with another class, it needs to be 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;
}

This way it can have 20 different attributes, but if the code is equal, it returns true and works with the .retainAll().


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!

That change in the .equals() affects all methods you compare objects, so if you use any .indexOf() or even a .contais() what prevails is yours equals superscript, that is, just comparing the code.

Browser other questions tagged

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