1

Is it possible to create method that will loop a List of any custom object type and then build a csv string from that?

So far, I have a method signature like so:

loopData(List<T> records){

}

I'd then like to loop each field in the object without having to specify the Object type.

So far this is what I am seeing, but it seems to explicitly state the object type, as in ClassABC:

ClassABC abc = new ClassABC();//!!explicitly stating custom object type!!!
for (Field field : abc.getClass().getDeclaredFields()) {
    field.setAccessible(true);
    String name = field.getName();
    Object value = field.get(abc);
    System.out.printf("%s: %s%n", name, value);
}

Is there any way to achieve this? Loop each field without explicitly stating the object type?

How about something like this:

for(T o : records){
            for(Field field:o.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                String name = field.getName();
               Object value = null;
               try {
                   value = field.get(o);
               } catch (IllegalArgumentException | IllegalAccessException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
               System.out.printf("%s: %s%n", name, value);
            }
}

Is this loop consistent? will it loop the object fields in the same order every time?

5
  • 1
    Actually your loop loops the fields of your abc object, the declaration and initialization of abc is out of the scope of the loop. Feb 1, 2019 at 21:21
  • check my new comments added to the post.
    – user1098063
    Feb 1, 2019 at 21:30
  • If you are planning to write any type of objects to a CSV file, how will you know how to read the data correctly? Feb 1, 2019 at 22:04
  • 1
    "Is this loop consistent? will it loop the object fields in the same order every time?" I do not know, but intuitively I think it is highly probable, because those fields are somewhere and are accessed somehow, probably with a deterministic order. However, if you are afraid of possible randomity, then you could store class name and column names the first time you generate a CSV and whenever the class is to be exported, you can read the fields to export in their specific order. Feb 2, 2019 at 0:45
  • 1
    Also, you can read the field names and order them alphabetically, so they will be in the very same order. Feb 2, 2019 at 0:46

1 Answer 1

0

In the end, this is what works...

I can get the field names and send them DOMO for creation, like so:

    List<Column> domoColumns = new ArrayList<Column>();
            for(Field field:o.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                String name = field.getName();
                domoColumns.add(new Column(ColumnType.STRING,name));
            }
return domoColumns;

Then I create the CSV like so:

StringBuilder stringBuilder = new StringBuilder();
    for (T o : records) {

        int numberOfFields = o.getClass().getDeclaredFields().length;
        int counter = 1;

        Field[] f = o.getClass().getDeclaredFields();
        for (int i = 0; i < o.getClass().getDeclaredFields().length; i++) {
            f[i].setAccessible(true);
            Object value = null;
            try {
                value = f[i].get(o);

                if (counter < numberOfFields) {
                    stringBuilder.append("\"").append(value).append("\"").append(",");
                } else {
                    stringBuilder.append("\"").append(value).append("\"").append(System.getProperty("line.separator"));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            counter++;
        }


    }

Your Answer

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