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?