Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!