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

362
Visualizações
How to map multiple variable in Java lambda?

I have a method that filters in a List containing enums i

if (availableImageSizes.stream()
                       .filter(t -> t.getNameSuffix().equals("-xs"))
                       .findFirst().isPresent())
{
    int width = availableImageSizes.stream()
                       .filter(t -> t.getNameSuffix().equals("-xs"))
                       .findFirst().getWidth();

    int height = availableImageSizes.stream()
                       .filter(t -> t.getNameSuffix().equals("-xs"))
                       .findFirst().getHeight();
}

As you see I try to filter the list according to its NameSuffix and after filtering I want to map that value's width and height to a variable. But the code is wrong and even if it works, it is so ugly.

Can I map multiple variable in one lambda? If so, how can I achieve this?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you need the values as part of the method's return value

If there is a sensible default, I'd do it like this

Optional<ImageSize> xsImageSize = availableImageSizes.stream()
    .filter(t -> t.getNameSuffix().equals("-xs"))
    .findFirst();
int height = xsImageSize.map(ImageSize::getHeight).orElse(-1);
int width = xsImageSize.map(ImageSize::getWidth).orElse(-1);

Alternatively, nullable Integers

Integer height = xsImageSize.map(ImageSize::getHeight).orElse(null);
Integer width = xsImageSize.map(ImageSize::getWidth).orElse(null);

Otherwise

Optional<ImageSize> xsImageSize = availableImageSizes.stream()
    .filter(t -> t.getNameSuffix().equals("-xs"))
    .findFirst();
if (xsImageSize.isPresent())
{
    int height = xsImageSize.get().getHeight();
    int width = xsImageSize.get().getWidth();
    //...
}

If you don't need those values for the return

availableImageSizes.stream()
    .filter(t -> t.getNameSuffix().equals("-xs"))
    .findFirst()
    .ifPresent(imageSize -> {
        int height = imageSize.getHeight();
        int width = imageSize.getWidth();
        //...
    });

or

availableImageSizes.stream()
    .filter(t -> t.getNameSuffix().equals("-xs"))
    .findFirst()
    .ifPresent(this::processXsImageSize);

//...
private void processXsImageSize(ImageSize imageSize) {
    int height = imageSize.getHeight();
    int width = imageSize.getWidth();
    //...
}
over 4 years ago · Santiago Trujillo Relatório

0

First of all, your goal is to remove duplicated code:

availableImageSizes.stream()
                   .filter(t -> t.getNameSuffix().equals("-xs"))
                   .findFirst()

I am not sure what "in one lamba" means. However, you can achieve this with a single chain of Stream and Optional method calls. However, you are not able to retrun two or more values at once. The solution is to return either an array to be decomposed to width and height. In better case use a custom object Dimension/Point class or Pair/AbstractMap.SimpleEntry.

The arrays is the easiest way to show the solution.

Integer[] dimensions = availableImageSizes.stream()
    .filter(t -> t.getNameSuffix().equals("-xs"))                // images with a suffix
    .findFirst()                                                 // Optional<MyImage>
    .map(obj -> new Integer[] {obj.getWidth(), obj.getHeight()}) // map dimensions
    .orElse(new Integer[] {null, null});                         // else default values
        
Integer width = dimensions[0];                                   // destructured width
Integer height = dimensions[1];                                  // destructured height
over 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