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 6856879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:56:37+00:00 2026-05-27T01:56:37+00:00

I have a java Hash, the structure is like this: HashMap<Integer, ArrayList<String>> finalMap =

  • 0

I have a java Hash, the structure is like this:

HashMap<Integer, ArrayList<String>> finalMap = new HashMap<Integer, ArrayList<String>>();

The finalMap.toString() is some thing like this

{0=[a1, a2, a3, a4], 1=[b1, b2, b3], 2=[c1, c2], 3=[d1]}

I need to generate all combination like this:

1. a1 b1 c1 d1
2. a1 b1 c2 d1
3. a1 b2 c1 d1
4. a1 b2 c2 d1
5. a1 b3 c1 d1
6. a1 b3 c2 d1
...
...
...
...
...

Thanks 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-27T01:56:37+00:00Added an answer on May 27, 2026 at 1:56 am

    Simply brute over all elements and skip duplicates (done by HashSet automatically). There are better ways to concat strings but for keeping it simple:

    HashSet<String> result = new HashSet<String>();
    
    for (String a : finalMap.get(0)) {
        for (String b : finalMap.get(1)) {
            for (String c : finalMap.get(2)) {
                for (String d : finalMap.get(3)) {
                    result.add(a + b + c + d);
                }
            }
        }
    }
    System.out.println(result);  
    

    Output should be like
    [a3b2c1d1, a1b3c1d1, a1b1c1d1, a1b2c2d1, a4b1c2d1, a4b3c2d1, a1b2c1d1, a2b1c2d1, a2b2c2d1, a3b3c2d1, a1b1c2d1, a1b3c2d1, a2b3c2d1, a3b3c1d1, a2b2c1d1, a4b1c1d1, a4b3c1d1, a4b2c1d1, a3b1c2d1, a2b3c1d1, a3b1c1d1, a4b2c2d1, a2b1c1d1, a3b2c2d1]

    However if you do not know the total amount of lists yet or if they may vary you could use a recursive approach

    void generate(HashSet<String> resultList, String resultString,
            int listNum, HashMap<Integer, ArrayList<String>> data) {
        if (listNum == 0) {
            // start: begin a new resultString
            for (int i = 0; i < data.get(listNum).size(); i++) {
                generate(resultList, data.get(listNum).get(i), listNum + 1,
                        data);
            }
        } else if (listNum == data.size() - 1) {
            // end: store completed resultStrings
            for (int i = 0; i < data.get(listNum).size(); i++) {
                resultList.add(resultString + data.get(listNum).get(i));
            }
        } else {
            // middlepart: append current string to given resultString
            for (int i = 0; i < data.get(listNum).size(); i++) {
                generate(resultList, resultString + data.get(listNum).get(i),
                        listNum + 1, data);
            }
        }
    }  
    

    called as follows:

    HashSet<String> result = new HashSet<String>();
    generate(result2, null, 0, finalMap);
    System.out.println(result);  
    

    Output should also be like
    [a3b2c1d1, a1b3c1d1, a1b1c1d1, a1b2c2d1, a4b1c2d1, a4b3c2d1, a1b2c1d1, a2b1c2d1, a2b2c2d1, a3b3c2d1, a1b1c2d1, a1b3c2d1, a2b3c2d1, a3b3c1d1, a2b2c1d1, a4b1c1d1, a4b3c1d1, a4b2c1d1, a3b1c2d1, a2b3c1d1, a3b1c1d1, a4b2c2d1, a2b1c1d1, a3b2c2d1]

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

Sidebar

Related Questions

I have this pseudo-code in java: bytes[] hash = MD5.hash(example); String hexString = toHexString(hash);
I have this custom function to calculate MD5 hash, written in Java. I can't
I have a function that generates a MD5 hash in C# like this: MD5
I'd like to save some hash objects to a collection (in the Java world
I have Some Passwords which contain some special and methimatical chracters like this password
I have been working with Java's HashMap lately and have run afoul of some
I have a java class that contains a hash map as a member. This
Is there some data structure in Perl like LinkedHashMap in java? Or something LRU
I have java method which encrypts a string: public String getCurrency(String hash){ try {
I have java script code to set some of the properties of ajax controls.

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.