java – How to create a filter from the words/phrases of interest to filter a specific vacancy from a "List"?

Question:

In my example I have two classes that are SetorInteresse and Vaga , below is the structure of the two:

Class SectorInterest:

public class SetorInteresse {

    private List<String> setores;

    public SetorInteresse(List<String> setores) {
        this.setores = setores;
    }

    public SetorInteresse() { }

    public void addPalavra(String palavra) {  setores.add(palavra); }

    public void removePalavra(String palavra) { setores.remove(palavra); }

    public List<String> getSetores() { return setores; }
}

Vacancy Class:

public class Vaga {
    private String tituloVaga;
    private String setor;
    private String funcao;    

    public Vaga(String tituloVaga, String setor, String funcao) {
        this.tituloVaga = tituloVaga;
        this.setor = setor;
        this.funcao = funcao;        
    }

    public Vaga() { }   

    public String getDescricaoVaga() {
        return tituloVaga;
    }

    public void setDescricaoVaga(String tituloVaga) { this.tituloVaga = tituloVaga; }

    public String getSetor() { return setor; }

    public void setSetor(String setor) { this.setor = setor; }

    public String getFuncao() { return funcao; }

    public void setFuncao(String funcao) { this.funcao = funcao; }   
}

Below I have two methods, one that populates the variable vagas of type List<Vaga> and the other that populates the setores attribute of the SetorInteresse object, see:

Method that populates the vagas variable:

List<Vaga> vagas = criaVagas();
...
static List<Vaga> criaVagas() { 
    List<Vaga> vagas = new ArrayList<>();
    vagas.add(new Vaga("Desenvolvedor Java", "Tecnologia da Informação", "Desenvolvedor"));       
    vagas.add(new Vaga("Desenvolvedor C# e Web", "Tecnologia da Informação", "Desenvolvedor"));
    vagas.add(new Vaga("Motorista Carreteiro", "Logistica", "Motorista"));       
    vagas.add(new Vaga("Gerente de Sistemas", "Tecnologia da Informação", "Desenvolvedor"));
    vagas.add(new Vaga("Estágiario Tecnologia da Informação", "Tecnologia da Informação", "Estágiario"));       
    vagas.add(new Vaga("Analista de Sistemas", "Tecnologia da Informação", "Analista"));              
    vagas.add(new Vaga("Suporte Técnico", "Suporte", "Suporte"));              
    vagas.add(new Vaga("Gerente Comercial", "Departamento Administrativo", "Gerente"));       
    vagas.add(new Vaga("Assistente de Recursos Humanos", "Recursos Humanos RH", "Aissistente"));       
    return vagas;
}

Method that populates the setores attribute:

SetorInteresse setorInteresse = criaSetorInteresse();
...
static SetorInteresse criaSetorInteresse() { 
    SetorInteresse setorInteresse = new SetorInteresse();
    setorInteresse.addPalavra("Desenvolvimento de programas");        
    setorInteresse.addPalavra("Tecnologia da informação e serviços");
    setorInteresse.addPalavra("Análise de sistemas");        
    return setorInteresse;
}

Based on the data that was entered in the two variables vagas and setorInteresse I would like to know if there is any way in which I could create a filter that returns me only the objects of the vagas list in which the value of the setor attribute of the Vaga object is does it relate to any of the words or phrases of the setores attribute or is there any alternative to this?

For example, if I have the following value in my sectors attribute:

Program development

I would get all objects of type Vaga where the value of the setor attribute is related to Desenvolvimento de programas , in this case the vacancies I would receive would be:

Java developer
C# and Web Developer
Systems manager
Information Technology Intern
Systems Analyst
Technical support

Therefore, the vacancies displayed would be according to the interests defined in the setores attribute.

Is there a way to create a filter that returns these results or is there a library that does this for me. And also I would like to know what criteria should I define in the relationship between words/phrases and how to define them, if necessary?

Answer:

Since you are using Java 8, you can use lambda expressions . An example of how you could use them is:

List<Vaga> vagas = criaVagas();
List<Vaga> vagasFiltradas = vagas.stream().filter(vaga -> vaga.getSetor().contains("Tecnologia")).collect(Collectors.toList());

What the above snippet does is use a filter that can be defined as you want and it will be applied to all elements in the list! The filter I chose as an example maps an object "vacancy" from the Collection "vacas" and checks if the sector of this vacancy is associated with a sector that you will use as a filter. The check looks to see if the job's sector field has the word "Technology" anywhere in the String . I put "Technology" to illustrate and then in this case they would appear:

Desenvolvedor Java
Desenvolvedor C# e Web
Gerente de Sistemas
Estágiario Tecnologia da Informação
Analista de Sistemas

This filter can be more sophisticated, considering, for example, other information about the vacancy to further limit the results.

Scroll to Top