7

I have a row which I need to parse which looks like:

@UNIT,a,b,c,,,,d,e,,,

and I expect Java to store the following values in a list:

[0] => a
[1] => b
[2] => c
[3] => null
[4] => null
[5] => null
[6] => d
[7] => e
[8] => null
[9] => null
[10] => null

I want to comma separate the values by comma and replace empty values with null.

I archive most of the part with the following code:

metaObject.unit = Arrays.stream(line
        .split(","))
        .skip(line.startsWith("@UNIT,") ? 1 : 0)
        .map(String::trim)
        .map(s -> " ".equals(s) || "".equals(s) || "_".equals(s)? null : s)
        .collect(Collectors.toList());

Where metaObject.unit is defined as List<String> unit = new ArrayList<String>();

The problem is that Java ignores the empty elements which occure after the last non-empty element. The output I get in the given case is:

[0] => a
[1] => b
[2] => c
[3] => null
[4] => null
[5] => null
[6] => d
[7] => e

I do not understand why Java does not threat the empty elements as before. Is there any trick to get this fixed?

8

The problem actually comes from line.split(",").

By default, quoting the Javadoc of split(regex):

Trailing empty strings are therefore not included in the resulting array.

So the result of this call is [@UNIT, a, b, c, , , , d, e].

What you want is to include the trailing empty strings, so you need to call line.split(",", -1) (note the negative second parameter). Quoting the Javadoc of split(regex, limit), about the limit parameter:

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

Corrected code:

metaObject.unit = Arrays.stream(line
    .split(",", -1))
    .skip(line.startsWith("@UNIT,") ? 1 : 0)
    .map(String::trim)
    .map(s -> s.isEmpty() || "_".equals(s) ? null : s)
    .collect(Collectors.toList());

(Note that I removed the call to " ".equals(s) since if that were the case, the preceding call to trim would have made that String an empty String, and, as Holger points out in his comment, I replaced "".equals(s) with the cleaner s.isEmpty()).

4
  • 3
    And then you can replace "".equals(s) with s.isEmpty() which is much cleaner, imho. – Holger Oct 7 '15 at 16:28
  • @Holger, for me the much cleaner would be to create a separate method like boolean isEmptyEntry(String s) { return s.isEmpty() || s.equals(" ") || s.equals("_"); }, so it can be separately supported, tested and reused. Btw in current code s.equals(" ") is never true as happens after trim. – Tagir Valeev Oct 8 '15 at 0:53
  • @Drudge, if your IDE has auto-completion the second parameter should show up in the suggested results whenever you type .spl␣. Actually studying the docs is half the battle ;) – the8472 Oct 8 '15 at 1:00
  • @Tagir Valeev: it has already been addressed in the answer that the string can never be " ". I’m not sure whether s.isEmpty() || s.equals("_") exceeds the threshold to deserve a testable method on its own. Btw., you could also write s.matches("_?") to test everything with one call, however, it might feel like bearing some overhead… – Holger Oct 8 '15 at 8:07

Your Answer

By clicking ā€œPost Your Answerā€, you agree to our terms of service, privacy policy and cookie policy

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