I created a CSV export that works like the code below. There is a LinkedHashMap where the keys are the column title and values are functions where certain properties are read.
By reording the lines where entries are added to the map, one also reorders the csv column representation. Column header and data are connected so that one can't move one without the other.
Are there any downsides with my code? Is there a better approach? (I omitted escaping characters and so on to reduce code)
public class Test {
private final static Map<String, Function<Bean, Object>> DEF_MAP = new LinkedHashMap<>();
public static void main(String[] args) {
DEF_MAP.put("Prop B", bean -> bean.getB());
DEF_MAP.put("Prop A", bean -> bean.getA());
List<Bean> beans = new ArrayList<>();
Bean a = new Bean();
a.setA("a1");
a.setB("b1");
beans.add(a);
Bean b = new Bean();
b.setA("a2");
b.setB("b2");
beans.add(b);
DEF_MAP.keySet().forEach(k -> {
System.out.print(k + ";");
});
System.out.println();
beans.forEach(bean -> {
DEF_MAP.values().forEach(v -> {
System.out.print(v.apply(bean) + ";");
});
System.out.println();
});
}
private static class Bean {
private String a;
private String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
}
-
CSV generally uses commas for separation rather than semicolonsEwan– Ewan2019年01月11日 08:06:33 +00:00Commented Jan 11, 2019 at 8:06
-
1Depends on the country you live in :)ave4496– ave44962019年01月11日 08:08:34 +00:00Commented Jan 11, 2019 at 8:08
-
mmmmmmmm.... pretty sure comma is almost universally used as the first separator. tools.ietf.org/html/rfc4180#section-2Ewan– Ewan2019年01月11日 08:21:31 +00:00Commented Jan 11, 2019 at 8:21
-
1@Ewan: It does and that is a terrible idea because so many languages use commas as a decimal separator which is the normal practice in many countries is to use semicolons instead. I would encourage everyone to go a step further and switch to tab-separators.anon– anon2019年01月11日 10:51:40 +00:00Commented Jan 11, 2019 at 10:51
-
1@Ewan: Yes, you can. That doesn't stop using commas being a terrible idea.anon– anon2019年01月11日 12:27:07 +00:00Commented Jan 11, 2019 at 12:27
1 Answer 1
There is a #codereview site, which might be better suited to your question format.
However! architecturally it has a few problems
First and foremost you have not separated out the code that writes the CSV into its own class. You need to do this and publish the code as a library.
Once you have done that you can address the other problems.
- You have no way of specifying the separator character to use
- You have no way of specifying different output streams
- You don't address escaping. I know you say why, but this is a huge part of CSV output.
- It's a lot of work for someone using the code to set up all the fields. I would want a default which automatically detected the public fields and used them in the order they appear on the Object array I pass in.
- I would also want to specify the default formats for numbers, dates etc
-
I focussed on how to manage the definition how the data is display. but thanks for the useful input. Can this question be moved? I confused the sitesave4496– ave44962019年01月11日 08:53:27 +00:00Commented Jan 11, 2019 at 8:53