Skip to content
Advertisement

How to simplify String concat in java?

Given the following code:

JavaScript

I am trying and discussing in the team how the simpliest code could look like while one could use also constructs like:

  • org.apache.commons.lang3.StringUtils: defaultString()
  • MoreObjects.firstNonNull(user.getVorname(), Strings.EMPTY)

A possible test could be like (expected results are visible here too):

JavaScript

Any ideas welcome… what could I try?

BTW: java 17 is allowed 🙂

The following code seemss to be very close but does not handle the braces for email if existing:

JavaScript

Advertisement

Answer

Since you have duplicated code to transform vorname, nachname and userId you may want to extract the logic to a Function or an UnaryOperator because it is a string to string transformation and an additional one for the email. Example

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement