How to filter objects with two lists using java 8 stream?

oitathi :

I need to filter a list with a large amount of objects. For each of these objects I need to verify if any of the parameters contain one of the words that are in other lists. I have developed a method to do this, but it is taking too long, I would like to know if there is a more efficient way to do this.

The main idea could be written in sql for better understanding:

SELECT * FROM PROJECT P WHERE P.NAME LIKE "%JAVA%" OR P.NAME LIKE "%PASCAL%" OR P.PRODUCT LIKE "%JAVA%" OR P.PRODUCT LIKE "%PASCAL% OR. P.ADDRESS LIKE "%JAVA" OR P.ADDRESS LIKE "%PASCAL%";

In Java I wrote in this way:

private List<Projeto> filtraResultado(List<Projeto> projetosAssinados, String[] filtros){
List<Projeto> result = new ArrayList<Projeto>();
for(Projeto p: projetosAssinados) {
    if(existeFiltroBuscadoNosCamposDePesquisa(p.getDsProjeto(), filtros) ||
       existeFiltroBuscadoNosCamposDePesquisa(p.getNomeProjeto(), filtros) ||
       existeFiltroBuscadoNosCamposDePesquisa(p.getSetor(),filtros) ||
       existeFiltroBuscadoNosCamposDePesquisa(p.getUn(), filtros) ||
       existeFiltroBuscadoNosCamposDePesquisa(p.getProcessosModelados(),filtros)||
       existeFiltroBuscadoNosCamposDePesquisa(p.getServicosPrestados(),filtros) ||
       existeFiltroBuscadoNosCamposDePesquisa(p.getTecnologias(),filtros)||
       existeFiltroBuscadoNosCamposDePesquisa(p.getDetalhamento(),filtros)) {
            result.add(p);
    }

}
return result;

}

public boolean existeFiltroBuscadoNosCamposDePesquisa(String campoPesquisado,String[] filtros ){
if(campoPesquisado == null) {
    return false;
}
for(String f: filtros) {
    if(StringUtils.containsIgnoreCase(campoPesquisado, f.trim())) {
        return true;
    }
}
return false;

}

stackFan :

For comparing two List you'd need anyMatch() and stream between two lists. But if you are checking against a filter I assume each records on that list are different and this can easily be converted to a use Set which will reduce time and be lot faster.

For example:

List< String > filterList = Arrays.asList( "z" );
List< String > toCheckList = Arrays.asList( "z", "b", "a", "q", "w" );

boolean contains = toCheckList.stream( ).anyMatch( list -> filterList.stream( ).anyMatch( filter -> StringUtils.containsIgnoreCase( filter, list ) ) );

    if ( contains ) {
        System.out.println( "Contains" );
        //your logic
    }

If your filter is a Set:

Set< String > filterList = Arrays.asList( "z", "b" ).stream( ).collect( Collectors.toSet( ) );
List< String > toCheckList = Arrays.asList( "z", "b", "a", "q", "w" );
boolean contains = toCheckList.stream( ).anyMatch( list -> filterList.contains( list ) );

    if ( contains ) {
        System.out.println( "Contains" );

        //your logic
    }

Give it a thought if your filter List can be changed to Set on your code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140224&siteId=1