Pankaj Kumar Katiyar
Pankaj Kumar Katiyar

Reputation: 1494

How to get all combination of values in a Treemap

I've a map containing values like:

TreeMap<String, String> params = new TreeMap<>();
        params.put("Has GPS – based Lat/Long",  "Yes, No");
        params.put("Ad Size", "320x480, 300x250");
        params.put("Integration", "Direct, Indirect");
        params.put("Vdo Ad Formats", "Max, Min, Med");
        params.put("App/Mobile Targeting", "Mobile Web, App");

Now I want all combination from values like:

320x480, Yes, Direct, Max, Mobile Web
320x480, Yes, Direct, Max, App
300x250, Yes, Direct, Max, APP
300x250, Yes, Indirect, Max, Mobile Web
300x250, Yes, Direct, Max, Mobile Web
300x250, No, Direct, Max, Mobile Web
etc....

Solution tried, its not giving me all combinations at all.

        List<String> keysList = new ArrayList<>();
        keysList.addAll(params.keySet());

        //1. iterating the keys list
        for(int i=0; i<keysList.size(); i++)
        {   
            String x = "";
            String [] values_00 = map.get(keysList.get(i)).split(",");

            //2. iterating array of values
            for(int a0=0; a0<values_00.length; a0++)
            {
                //3. Iterating the next available keys from the list
                for(int j=i+1; j<keysList.size(); j++)
                {
                    String [] values_01 = map.get(keysList.get(j)).split(",");

                    //4. Iterating values of next array of values of next available keys
                    for(int a1=0; a1<values_01.length; a1++)
                    {
                        x = values_00[a0] + "  " + values_01[a1];
                        System.out.println(x);
                    }
                }
            }
        }

Upvotes: 0

Views: 223

Answers (2)

Tagir Valeev
Tagir Valeev

Reputation: 100279

Not very difficult with Java-8:

Stream<String> combinations = params.values().stream()
        .<Supplier<Stream<String>>>map(str -> () -> Pattern.compile(", ").splitAsStream(str))
        .reduce((s1, s2) -> () -> s1.get().flatMap(e1 -> s2.get().map(e2 -> e1 + ", " + e2)))
        .get().get();
combinations.forEach(System.out::println);

The output is:

320x480, Mobile Web, Yes, Direct, Max
320x480, Mobile Web, Yes, Direct, Min
320x480, Mobile Web, Yes, Direct, Med
320x480, Mobile Web, Yes, Indirect, Max
320x480, Mobile Web, Yes, Indirect, Min
...
300x250, App, No, Indirect, Max
300x250, App, No, Indirect, Min
300x250, App, No, Indirect, Med

Note that the map elements were reordered as you're using TreeMap which sorts by key. If you need some specific order, use LinkedHashMap instead.

Upvotes: 2

Raffaele
Raffaele

Reputation: 20885

This kind of problems is usually solved with recursive programs. I put a runnable example on GitHub. To understand what it does, basically you want to print every option value for every other options values.

You'll have a buffer of length equal to the numbef of options, and one (recursive) call for each option. In the recursive function

  • when the buffer is full, you'll print the result out
  • otherwise, loop over the values in the option and call the recursive function again

Upvotes: 0

Related Questions