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

189
Visualizações
Combining Two Different Lists Holding Different Objects in to Third List holding Object as Cross Join of Two Lists

I have List of Objects. Each object holds a List of Long and a List of JsonNode. What I am trying to do is create a third List with Object, wherein each Object is the result of a cross join between the above two Lists.

E.g.

class MyClass{
    List<Long> _listLong;
    List<JsonNode> _listJsonNode;
}

I have List of MyClass Objects. What I want is to create a List of MyClass.

class MyClass2{
    Long id;
    JsonNode jsonNode;
}

such that List<MyClass2> holds the result of a cross join of List<Long> and List<JsonNode>.

I am able to do the same using loops. Is there a way to achieve the same using Streams in Java?

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

0

If you're starting with a list of MyClass objects, you have to flatten it and use two nested streams for the Cartesian product.

List<MyClass2> result = myClassList.stream().flatMap(
   mc -> mc.get_listLong()
                .stream()
                .flatMap(i -> mc.get_listJsonNode()
                                .stream()
                                .map(js -> new MyClass2(i, js))))
        .collect(Collectors.toList());

That assumes the constructor MyClass2(Long, JsonNode) exists. The inner flatMap is creating the required Cartesian product, by essentially iterating over all JsonNode elements for each Long element.

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