Passing Java 8 translation function as parameter
I have a comma separated string and I want to convert it to an array. However, in some cases I need holistic parsing, sometimes double. Is there a way that I can pass mapToDouble or mapToInt and not write the whole thing again.
return Arrays.stream(test.split(",")).mapToDouble(x -> {
if (StringUtils.isEmpty(x)) {
return condition ? -1 : 0;
}
return Double.parseDouble(x);
}).toArray();
return Arrays.stream(test.split(",")).mapToInt(x -> {
if (StringUtils.isEmpty(x)) {
return condition ? -1 : 0;
}
return Integer.parseInt(x);
}).toArray();
Is there a way to do this in a function where I can have a common function and create a storage for the corresponding array?
source to share
You can do this with a simple function that takes String
and Function<String, T>
that will transform each element of the string using this function. The good news is that this function can return any type you want: Integer
, Double
, BigDecimal
, String
or any other type that you want. In the example below, I am using a method reference like:
-
Integer::valueOf
to convert items to valuesInteger
-
Double::valueOf
to convert items to valuesDouble
-
String::valueOf
to convert items to valuesString
Consider the following example:
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ParsingStringTest {
public static void main(String[] args) {
String str = "1, , 3, 4, 5, , 7, sasd, aaa, 0";
List<Double> doubles = parse(str, Double::valueOf);
List<Integer> integers = parse(str, Integer::valueOf);
List<String> strings = parse(str, String::valueOf);
System.out.println(doubles);
System.out.println(integers);
System.out.println(strings);
Double[] array = doubles.toArray(new Double[doubles.size()]);
System.out.println(Arrays.toString(array));
}
public static <T> List<T> parse(String str, Function<String, T> parseFunction) {
return Arrays.stream(str.split(","))
.filter(s -> !s.isEmpty())
.map(s -> {
try {
return parseFunction.apply(s.trim());
} catch (Exception e) {}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
Console output for the following example:
[1.0, 3.0, 4.0, 5.0, 7.0, 0.0]
[1, 3, 4, 5, 7, 0]
[1, , 3, 4, 5, , 7, sasd, aaa, 0]
[1.0, 3.0, 4.0, 5.0, 7.0, 0.0]
Hope this helps.
source to share
There is a way, but you won't be working with primitives anymore, because generics in Java doesn't support primitive types. You can use wrapper types instead Integer
and Double
:
public static <T> T[] convert(
String test,
Function<String, T> parser,
boolean condition,
T ifConditionTrue,
T ifConditionFalse,
IntFunction<T[]> arrayGenerator) {
return Arrays.stream(test.split(","))
.map(x -> {
if (StringUtils.isEmpty(x)) {
return condition ? ifConditionTrue : ifConditionFalse;
}
return parser.apply(x);
})
.toArray(arrayGenerator);
}
This method can be used like this:
Integer[] ints = convert("1, ,3", Integer::parseInt, true, -1, 0, Integer[]::new);
source to share