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;
}
filtros, is applicable for all those variables instead of one group of filter per variable. Now you are comparing everything to everything so to speak. Maybe you could re-design this part somehow?