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();

        Bean b = new Bean();

        DEF_MAP.keySet().forEach(k -> {
            System.out.print(k + ";");

        beans.forEach(bean -> {
            DEF_MAP.values().forEach(v -> {
                System.out.print(v.apply(bean) + ";");

    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 semicolons
    – Ewan
    Commented Jan 11, 2019 at 8:06
  • 1
    Depends on the country you live in :)
    – ave4496
    Commented Jan 11, 2019 at 8:08
  • mmmmmmmm.... pretty sure comma is almost universally used as the first separator. tools.ietf.org/html/rfc4180#section-2
    – Ewan
    Commented 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. Commented Jan 11, 2019 at 10:51
  • 1
    @Ewan: Yes, you can. That doesn't stop using commas being a terrible idea. Commented 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.

  1. You have no way of specifying the separator character to use
  2. You have no way of specifying different output streams
  3. You don't address escaping. I know you say why, but this is a huge part of CSV output.
  4. 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.
  5. 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 sites
    – ave4496
    Commented Jan 11, 2019 at 8:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.