5

I want to know if there is any way to convert a HashMap to a DataTable in java Cucumber. I tried doing some googling and saw that the reverse is possible. Any idea on how to implement this?

Thanks in advance.

  • Do you have a single row in your data table, wherein headers are keys and row items are values of the map in question? – gargkshitiz May 04 '18 at 04:21
  • My map will have 1 key-value pair and I want to convert that to a DataTable with headers. For example - if my map has ("name", "John") I want to be converted as DataTable: | Firstname | | John | – Srikkanth Govindaraajan May 04 '18 at 04:23

1 Answers1

3

Considering that

  1. All the data is kept in the map as Strings and
  2. You want a single row in your data table, wherein headers would be keys and row items would be values from the input hashmap. If the conversion would have been other way around, then you would have a list of Maps as Data table would have multiple rows (values for different maps) against the columns (keys).

You could try:

List<List<String>> data = Arrays.asList(new ArrayList<String>(map.keySet()), new ArrayList<String>(map.values())); 
DataTable dataTable = DataTable.create(data);

P.S. I have not tested the syntax.

gargkshitiz
  • 2,130
  • 17
  • 19