-2

I have this map:

static Map<String, String> myMap = new HashMap<>();

static {
    myMap.put("param1","param11");
    myMap.put("param2","param22");
}

and I have this 2d array:

Object[][] objArr1 = new Object[][]{
             {"1", 1},
             {"2", 2}
    };

I want to "merge" the above into another 2d array:

Object[][] objArr3 = new Object[][];

So the resulting content of objArr3 is (in no particular order):

 {"1", 1, "param1","param11"},
 {"1", 1, "param2","param22"},

 {"2", 2,"param1","param11"},
 {"2", 2,"param2","param22"

I understand that I probably need

new Object[objArr1 * myMap.size()][4];

but can't create a proper nested for loop to do this. I've tried to combine solutions from here and here but to no avail. Any help greatly appreciated.

Andrejs
  • 10,803
  • 4
  • 43
  • 48

2 Answers2

2

This should work.
It iterates over all rows of the array, and all key/value pairs of the map, combining both of them in a new row. And then putting each row in the output-array.

static Map<String, String> map1 = new HashMap<>();

static {
    map1.put("param1","param11");
    map1.put("param2","param22");
}

static Object[][] array1 = new Object[][]{
            {"1", 1},
            {"2", 2}
};

public static void main (String[] args) throws java.lang.Exception
{
    // the new array has precisely N*M rows
    // where N is the number of rows in the input array
    // and M is the number of entries in the map
    Object[][] newArray = new Object[array1.length * map1.size()][4];
    int index = 0;
    for(int row=0;row<array1.length;row++)
    {
        for(Map.Entry<String, String> en : map1.entrySet())
        {
            Object[] newRow = new Object[4];
            newRow[0] = array1[row][0];
            newRow[1] = array1[row][1];
            newRow[2] = en.getKey();
            newRow[3] = en.getValue();
            newArray[index] = newRow;
            index++;
        }
    }
}

if I then output the array, I get

1 1 param1 param11
1 1 param2 param22
2 2 param1 param11
2 2 param2 param22

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
0

I needed an Object[][] as a final product for TestNg's @DataPovider, but I couldn't get around problems due to arrays being of fixed size. So I do all the manipulations with a list of lists and as a final step convert that to a 2d array:

public class main {

static Map<Object, Object> myMap = new HashMap<>();

static {
     myMap.put("1","1");
     myMap.put("2","2");
     myMap.put("3","3");
}


public static void main(String[] args){

    List<List<Object>> list = asList(
                         asList("param1", "param11"),
                         asList("param2", "param22")
                       );

    Object[][] finaList = mergeListWithMap(list, myMap);

    System.out.println(Arrays.deepToString(finaList));

}

private static Object[][] mergeListWithMap(List<List<Object>> list, Map<Object, Object> myMap) {
    List<List<Object>> newList = new ArrayList<>();

    for(List<Object> o : list){

         for(Map.Entry<Object, Object> en : myMap.entrySet()){
                List<Object> nl = new ArrayList<>(o); // copy original containing "param<x>", "param<xx>"
                nl.add(en.getKey());       // append               
                nl.add(en.getValue());     // append
                newList.add(nl);           // add final result to new list to be returned
         }
    }

    // Convert list of lists -> 2d-array
    Object[][] finaList = newList.stream()
            .map(l -> l.stream().toArray(Object[]::new))
            .toArray(Object[][]::new);

    return finaList;
}


private static <T> List<T> asList(T ... items) {
    List<T> list = new ArrayList<>();
    for (T item : items) {
        list.add(item);
    }
    return list;
}

}

Output:

 [param1, param11, 1, 1]
 [param1, param11, 2, 2]
 [param1, param11, 3, 3]
 [param2, param22, 1, 1]
 [param2, param22, 2, 2]
 [param2, param22, 3, 3]
Andrejs
  • 10,803
  • 4
  • 43
  • 48