Is it possible to compare items from two lists and if there is similarity between items do not display this item in a datatable?

Asked

Viewed 763 times

0

I have a list that is populated by data from an xml file and another list that is returned from a query in the database. I wonder if it is possible to compare an existing code in these 2 lists. If the codes are equal do not display this element in the datatable.

Example:

Lista 1 (xml): [processo[cod= 123, nome=Diego], processo[cod= 321, nome=Bruno]]
Lista 2 (bd):  [processo[cod=321, nome Bruno], processo[cod = 456, nome=josé]]

The code process 321 and name Bruno exists in both lists, so it is not displayed in the dataTable

Datatable:

Código              Nome

123                 Diego
456                 José

How do I get this result?

Bean:

@ManagedBean(name = "dtBasicView")
@ViewScoped
public class SolicitacoesBean {
private List<Solicitacoes> list;
private List<Solicitacoes> listaFiltrada;
    @PostConstruct
    public void init() {
        // pega a lista com os dados do .xml
                try {
                    list = XmlParserSolicitacoes
                            .realizaLeituraXML("C:\\Solicitacoes.xml");
                    System.out.println(list);

                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

    }
}

Method that returns the database query in a list:

public List<SolicitacoesBD> listar(){
        SolicitacoesDAO dao = new SolicitacoesDAO();
        List<SolicitacoesBD>lista = dao.listar();

        for(SolicitacoesBD s : lista){
            System.out.println(s);
        }

        return lista;
    }

My datatable:

<p:dataTable emptyMessage="Nenhum registro encontrado" var="lista"
                value="#{dtBasicView.list}"
                filteredValue="#{dtBasicView.listaFiltrada}" rows="10"
                paginator="true" style="margin-top: 5px;"
                rowKey="#{lista.codigoBeneficiario}">

                <p:column headerText="Código" filterBy="#{lista.codigoBeneficiario}" style="width:20%" >
                    <h:outputText value="#{lista.codigoBeneficiario}" />
                </p:column>

                <p:column headerText="Nome" filterBy="#{lista.nomePessoa}" style="width:45%;"
                    sortBy="#{lista.nomePessoa}">
                    <h:outputText value="#{lista.nomePessoa}" />
                </p:column>

                <p:column headerText="Senha" style="width:10%">
                    <h:outputText value="#{lista.senha}" />
                </p:column>

                <p:column headerText="Data Solic." style="width:20%">
                    <h:outputText value="#{lista.dataSolicitacao}" />
                </p:column>

                <p:column headerText="Status" style="width:22%" sortBy="#{lista.status}" filterBy="#{lista.status}">
                    <h:outputText value="#{lista.status}" />
                </p:column>

                <p:column headerText="Opções" style="width:7%">
                    <p:commandButton  icon="ui-icon-search"
                        action="#{dtBasicView.abrirDialogo}" process="@this">
                        <!--                Mandar informação para outra página -->
                        <f:setPropertyActionListener target="#{dtBasicView.solicitacoes}" value="#{lista}" />
                    </p:commandButton>
                </p:column>
            </p:dataTable>
  • If you use a list Set will not remove the repeated ones? or has different attributes?

  • It has different attributes, I must leave the same attributes?

  • A very shoddy solution would be to rotate 2 loops in the list and remove the repeated values.

  • But for that all the elements would have to be equal? Or can I just compare the code?

2 answers

1

I would do something like this:

private List<Solicitacoes> listDB;
private List<Solicitacoes> listXML;
private List<Solicitacoes> listaFiltrada;

private void filtrar() {
  listDB = dao.listar();
  listXML = XmlParserSolicitacoes.realizaLeituraXML("C:\\Solicitacoes.xml");
  List<Solicitacoes> list = new ArrayList<Solicitacoes>(listDB.size() + listXML.size());
  list.addAll(listDB);
  list.addAll(listXML);
  listaFiltrada = new LinkedList<Solicitacoes>();
  for (Solicitacoes s : list) {
    if (!listDB.contains(s) || !listXML.contains(s)) {
      listaFiltrada.add(s);
    };
  };
};

I did a little bit of thinking, but the logic is there. It’s not the most efficient way but it’s the easiest way to read.

  • I’ll take a look, thanks

0


A very ordinary solution would be

Iterator<Solicitacoes> iter = list.iterator();
while (iter.hasNext()) {
    Solicitacoes solicitacao = iter.next();

    for(Solicitacoes s : lista){
        if (s.getCodigoBeneficiario() == solicitacao.getCodigoBeneficiario()) {
            iter.remove();
        }
    }
}
  • Is making a mistake, Iterator<SolicitacoesBD> iter = list.iterator();

  • list is of type Requests

  • @Techies I updated the answer

  • Just make sure it’s getCodigoBeneficiario even

  • Is not entering the if

  • I’m trying to fix

  • 1

    Okay, I tidied up and it’s working perfectly. I just don’t really understand the code, so I’m going to do a little research on Iterator. Thank you very much

Show 2 more comments

Browser other questions tagged

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