Is it possible to make a comparison between items in two lists and if there is similarity between items not displaying this item in a dataTable?

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 would like to know if it is possible for me to compare existing code in these 2 lists. If the codes are the same, they 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 process of code 321 and name Bruno exists in both lists, so it does not appear in dataTable

DataTable:

Código              Nome

123                 Diego
456                 José

How do I get to 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>
    
asked by anonymous 09.06.2015 / 19:32

2 answers

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();
        }
    }
}
    
09.06.2015 / 20:23
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 half a head, but the logic is there. It's not the most efficient way but it's the easiest way to read it.

    
09.06.2015 / 20:32