Converting List to Two dimensional array Java for TestNG data provider

Converting List to Two dimensional array Java for TestNG data provider



If you are using TestNG you would find that to use a method as a data provider you have to create a method that returns a two dimensional Object array.



So if I have a List of (say) Students , is there any utility method to convert it into a two dimensional array.



I am NOT looking to convert it manually using a loop like this


List<Student> studentList = getStudentList();

Object objArray = new Object[studentList.size];

for(int i=0;i< studentList.size();i++)
objArray[i] = new Object[1];
objArray[i][0] = studentList.get(i);


return objArray;



Instead I am looking at a utility function if any is available in any of the libraries.



Or a better way of writing a data provider method for TestNG




3 Answers
3



This is not directly answer your question. But you can also solve that problem like this


@DataProvider
public Iterator<Object> studentProvider()
List<Student> students = getStudentList();
Collection<Object> data = new ArrayList<Object>();
students.forEach(item -> data.add(new Objectitem));
return data.iterator();



With Java 8 streams (and using this question about converting a stream into array):


@DataProvider
public Object studentProvider()
return studentList.stream()
.map(student -> new Object student )
.toArray(Object::new);



So be it ... let stackoverflow call me a tumbleweed ... but here is the answer.


List<Student> studentList = getStudentList();

Object objArray = new Object[studentList.size];

for(int i=0;i< studentList.size();i++)
objArray[i] = new Object[1];
objArray[i][0] = studentList.get(i);


return objArray



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế