Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:36:56+00:00 2026-05-24T21:36:56+00:00

I have the following challenge that I need to create unique object tuples out

  • 0

I have the following challenge that I need to create unique object tuples out of object lists. The speical challenge is here how I can do it for dynamic list sizes (n-dimension)? It is easy in case you have a fixed dimension. I hope somebody knows a third party API or has some hints for me how I can reach this.

The exampel below shows it for a 3 lists, so it is easier to explain.
I have the objects sorted by its class in a list e.g.

lista = {a1,a2}
listb = {b1,b2,b3}
listc = {c1,c2}

I like to have the following tuples, so that each object is paired once with each other:

tuple1  = {a1,b1,c1}
tuple2  = {a1,b1,c2}
tuple3  = {a1,b2,c1}
tuple4  = {a1,b2,c2}
tuple5  = {a1,b3,c1}
tuple6  = {a1,b3,c2}
tuple7  = {a2,b1,c1}
tuple8  = {a2,b1,c2}
tuple9  = {a2,b2,c1}
tuple10 = {a2,b2,c2}
tuple11 = {a2,b3,c1}
tuple12 = {a2,b3,c2}

How can I achieve that in case

– the number of lists is dynamically
– the size of lists is dynamically

any hints or ideas? Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:36:56+00:00Added an answer on May 24, 2026 at 9:36 pm

    We can build up to the solution as follows. First notice that the case in which the sizes of the lists vary is never a problem in modern languages, because virtually all languages today support the variable-sized list. The trickier part is when the number of lists vary.

    Here is the Python case for the fixed number of lists:

    lista = ["a1","a2"]
    listb = ["b1","b2","b3"]
    listc = ["c1","c2"]
    [(a,b,c) for a in lista for b in listb for c in listc]
    

    For Java do the same thing. You will need three nested for-loops:

    List<String> lista = Arrays.asList("a1","a2");
    List<String> listb = Arrays.asList("b1","b2","b3");
    List<String> listc = Arrays.asList("c1","c2");
    
    List<List<String>> result = new ArrayList<List<String>>();
    for (String a: lista) {
        for (String b: listb) {
            for (String c: listc) {
                result.add(Arrays.asList(a, b, c));
            }
        }
    }
    System.out.println(result);
    

    This yields

    [[a1, b1, c1], [a1, b1, c2], [a1, b2, c1], [a1, b2, c2], [a1, b3, c1], [a1, b3, c2], [a2, b1, c1], [a2, b1, c2], [a2, b2, c1], [a2, b2, c2], [a2, b3, c1], [a2, b3, c2]]
    

    Now the trick is how do you do an indeterminate number of for loops? One way is to use recursion.

    Start with the base case of 0 lists (or even 1) and then ask yourself: how does the result change when I add a new list? Then you can put together a nice little recursive formulation. Do you need help with this?

    (Aside: In Python we have itertools for these kinds of things. I am not aware of any Java analog, but some googling might turn something up.)

    Okay let’s develop the recursive formulation for an unbounded number of lists. If you have only only one list, say

    a = ["a1", "a2"]
    

    then the result is [["a1"],["a2"]]. But now let’s add another list:

    a = ["b1", "b2"]
    

    What is the answer now?

    [[a1, b1], [a1, b2], [a2, b1], [a2, b2]]
    

    How did we get that? The answer is we took each element of the new list and appended it to each element in the result. And every time we add a new list we will do the same. Suppose now we add ["c1","c2","c3"]. Do you see what we will get? If so, you should now be able to define the recursive step!

    COMPLETE CODE

    Okay I could not resist! Here you go.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * An application requested by a SO user.
     */
    public class ProductExample {
    
        /**
         * Returns a list containing all possible products of a list of lists of strings.
         */
        public static List<List<String>> product(List<List<String>> lists) {
            List<List<String>> result = new ArrayList<List<String>>();
    
            if (lists.isEmpty()) {
                result.add(new ArrayList<String>());
                return result;
            }
    
            List<List<String>> partial = product(lists.subList(0, lists.size() - 1));
            for (List<String> list: partial) {
                for (String s: lists.get(lists.size() - 1)) {
                    List<String> listCopy = new ArrayList<String>(list);
                    listCopy.add(s);
                    result.add(listCopy);
                }
            }
            return result;
        }
    
    
        public static void main(String[] args) {
            System.out.println(product(
                    Arrays.asList(
                            Arrays.asList("a1", "a2"),
                            Arrays.asList("b1", "b2", "b3"),
                            Arrays.asList("c1", "c2"),
                            Arrays.asList("d1", "d2"))));
        }
    }
    

    Rant: This is sooooooo much easier in Python. Or Ruby. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following challenge, and I haven't found a good answer. I am
I have a query that I need to execute that I do not know
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following text in a file 23456789 When I tried to replace the
I have following Code Block Which I tried to optimize in the Optimized section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.