-1

I want to optimize this code instead of using td(String.valueof(dataset.get())) mutliple times. I am relatively new to lambda expressions usage and not able to figure out a better way than this

Code need to be optimized

  return table(thead(tr(each(columnHeaders, header -> 
  th(String.valueOf(header))))),
  tbody(each(myList, dataset ->
  tr(td(String.valueOf(dataset.get(0))), 
  td(String.valueOf(dataset.get(1))), 
  td(String.valueOf(dataset.get(2))), 
  td(String.valueOf(dataset.get(3))), 
  td(String.valueOf(dataset.get(4))),
  td(String.valueOf(dataset.get(5))), 
  td(String.valueOf(dataset.get(6))), 
  td(String.valueOf(dataset.get(7))), 
  td(String.valueOf(dataset.get(8))), 
  td(String.valueOf(dataset.get(9))),
  td(String.valueOf(dataset.get(10)))

      ))
  )
)
  • What this supposed to create as output? And what prevents you from writing a loop statement? – GhostCat Mar 22 '19 at 19:28
  • Seems you want to create a DSL. Imho Java isn't the best language for that. – LppEdd Mar 22 '19 at 19:28
  • How many arguments does "tr" method have? – Ilya Mar 22 '19 at 19:29
  • What exactly do you want to optimize? Code style or performance? If it's the latter one, what performance problem do you have? – Pavel Smirnov Mar 22 '19 at 19:36
  • What library are you using? What is the method `each`? We can help a little better. If `dataset` is `Iterable`, then you can use `forEach` on it and do like you did with `columnHeaders` – xbakesx Mar 22 '19 at 19:55

2 Answers2

2

I see, you're using j2html.

I think you can do this in one line like this:

return table(thead(tr(each(columnHeaders, header -> th(String.valueOf( tbody(each(myList, dataset -> each(dataset, data -> td(data)))))

But it probably reads better if you break it out a little bit:

return table(thead(tr(each(columnHeaders, 
                           header -> th(String.valueOf(header))))),
             tbody(each(myList, 
                        dataset -> each(dataset, data -> td(String.valueOf(data))))));

All that I've done here, is inside your call to tbody you say each(myList..., then just do each again for every element of myList.

If you made sure that columnHeaders and myList were typed collections (like List<String>) then you could do something like this:

return table(thead(tr(each(columnHeaders, TagCreator::header))),
             tbody(each(myList, 
                        dataset -> each(dataset, TagCreator::td))));
xbakesx
  • 13,202
  • 6
  • 48
  • 76
0

Don't know what library you're using so have made an assumption for tr() and td() return types:

private TR trOf(List<?> dataset, int startIdx, int endIdxInclusive) {
    List<TD> tds = IntStream.rangeClosed(startIdx, endIdxInclusive).map(i -> tdOf(dataset, i)).collect(Collectors.toList());

    return tr(tds.toArray(new TD[0]));
}

private TD tdOf(List<?> dataset, int idx) {
    return td(String.valueOf(dataset.get(idx));
}

Then:

return table(thead(tr(each(columnHeaders, header -> 
th(String.valueOf(header))))),
tbody(each(myList, dataset ->
trOf(dataSet,0,10)

    ))
)
Not a JD
  • 1,864
  • 6
  • 14
  • Probably j2html, as that HTML builder library is tagged with the question. Thus I guess it is not his library, so your idea might not work for him. – GhostCat Mar 22 '19 at 19:57