Sal-laS
Sal-laS

Reputation: 11649

The easiest way to convert arrayList to object[][]

I am receiving my data from the database in the type of

Map<String,ArrayList<String>

So, the columns' names are the keys, and the columns values are stored in the

ArrayList<String>

I need to convert these hash to Object [][] data; for presenting data.

I know, how simple solutions like loops work. But i would like to know what is the shortest solution to convert

ArrayList<String> to Object[][] data

I am using java 8.

Let's say, at the beginning i have

Map<String, ArrayList<String>> res

Point:

I have the data in ArrayList<String> means that, I have access to data vertically, but i in Object [][] it is somehow horizontally.

As i said res is the database table, so Object [][] would be a row, meanwhile the ArrayList<String> is the column

Upvotes: 0

Views: 1535

Answers (2)

Devendra Lattu
Devendra Lattu

Reputation: 2802

Java 8 provides an efficient solution for converting it to a data object as stated well by @Gerald in the above answer.
However, if you are also interested in doing this in the naive approach then I have implemented the code as follows.

    Map<String, ArrayList<String>> map = new HashMap<>();
    String[] arr1 = { "I", "am", "good" };
    List<String> list = Arrays.asList(arr1);
    map.put("Max", new ArrayList<String>(list));

    arr1 = new String[]{"I", "am", "always", "excited"};
    list = Arrays.asList(arr1);
    map.put("Peter", new ArrayList<String>(list));

    System.out.println(map);
    // OUTPUT: {Max=[I, am, good], Peter=[I, am, always, excited]}

    Object[][] data = new Object[map.size()][];
    int i = 0;

    // iterate the map and store to Object data
    for(Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
        data[i] = new Object[entry.getValue().size() + 1]; // number of records from list + (One) map key

        data[i][0] = entry.getKey(); // save key at '0'th index
        int j = 1;                   // iterate the ArrayList to save values in the same row. 
        for(String s : entry.getValue()) {
            data[i][j++] = s;
        }
        i++;
    }

    // Printing the data object
    for(i = 0; i < data.length; i++) {
        for(int j = 0; j < data[i].length; j++) {
            System.out.print(data[i][j] + " ");
        }
        System.out.println();
    }

    // OUTPUT :
    //Max I am good 
    //Peter I am always excited

Upvotes: 0

Gerald M&#252;cke
Gerald M&#252;cke

Reputation: 11132

How about this?

Object[][] result = res.entrySet()
                       .stream()
                       .map(e -> e.getValue().toArray())
                       .collect(Collectors.toList())
                       .toArray(new Object[0][0]);

The resulting array has the dimension: result[col][row]. So when iterating:

for(int col = 0; col < result.length; col++) {
  for(int row = 0; y < result[col].length; row++){
     result[col][row]; //table cell 
  }
}

Upvotes: 2

Related Questions