Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

430
Visualizações
In Java 8, can I use streams to filter a partial string?

In Java 8, can I use streams to filter a partial string?

Let us assume I have a list of animals like:

Brown Bear
Black Bear
Black Crow
Red Herring 
Owl
Sparrow
Blackbacked Flocking Crow

Let us assume all of the Animals names are in a list of Animals Objects

public class Animal{
    public name;
    public animalType;
}

Is there some way to find all of the animals that have Black regardless of the case somewhere in the name. Something like the following...

List<Animal> filtList = employeeList.stream()
    .filter(x -> "%Black%".toUpperCase().equals(x.getLastName().toUpper()))
    .collect(Collectors.toList());
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

There is no toUpper() method for String.

It is toUpperCase() and also you need to use contains() to check "BLACK" there anywhere in the whole string, so the code should be simple as shown below:

List<Employee> filtList = inputList.stream().
         filter(value -> value.toUpperCase().//convert to uppercase for checking
         contains("BLACK")).//filter values containing black
         collect(Collectors.toList());//collect as list
about 4 years ago · Santiago Trujillo Relatório

0

Use regex:

List<Animal> filtList = list.stream()
   .filter(x -> x.getName().matches("(?i).*black.*"))
   .collect(Collectors.toList());

The regex flag "(?)" means "ignore case".

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda