1

How can I convert a Set<Set<String>> to a String[][]? I have tried .toArray(new String[0]) but this doesn't seem to do the trick.

Thanks! Christian

3 Answers 3

4

For each Set<String> s in the outer set, convert s to String[] and add it to your Array of arrays.

I don't know any built in way to create n-dimensional arrays from nested collections.

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't this painfully slow?
What do you think the implementation of Set.toArray() does internally? The JVM isn't magic.
@ChristianStewart why slow? it makes no difference if you loop or if java does it internally.
Well it isn't the perfect solution. If it's a bottleneck I'd go and stick with arrays. If it's not don't convert since arrays behave vastly different than sets. You would lose all the guarantees set gives you.
1

Loop through the set and create the array

Comments

0

Try below code, it works

package com.rais;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Rais.Alam
 * @date Dec 17, 2012
 */
public class SetClient
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        Set<Set<String>> myArray = new HashSet<Set<String>>();

        Set<String> arr1 = new HashSet<String>();
        Set<String> arr2 = new HashSet<String>();
        Set<String> arr3 = new HashSet<String>();

        arr1.add("a-1");
        arr1.add("a-2");
        arr1.add("a-3");

        arr2.add("b-1");
        arr2.add("b-2");
        arr2.add("b-3");

        arr3.add("c-1");
        arr3.add("c-2");
        arr3.add("c-3");
        arr3.add("c-4");
        arr3.add("c-5");

        myArray.add(arr1);
        myArray.add(arr2);
        myArray.add(arr3);

        String[][] outputArray = convertSetOfSetToArray(myArray);

        for (String[] outerArr : outputArray)
        {
            for (String value : outerArr)
            {
                if (value != null)
                {
                    System.out.println(value);
                }
            }
        }

    }

    public static String[][] convertSetOfSetToArray(Set<Set<String>> myArray)
    {
        int secondArraySize = 0;

        /*
         * Looping array to get the size.
         */
        for (Set<String> innerSet : myArray)
        {
            if (innerSet.size() > secondArraySize)
            {
                secondArraySize = innerSet.size();
            }
        }
        // Declaring and initializing String arrays;
        String[][] outputArray = new String[myArray.size()][secondArraySize];

        int firstIndex = 0;
        int secondIndex = 0;

        for (Set<String> innerSet : myArray)
        {
            for (String value : innerSet)
            {
                outputArray[firstIndex][secondIndex] = value;
                secondIndex++;
            }
            secondIndex = 0;
            firstIndex++;
        }

        return outputArray;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.