Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda