I have a string which is comma separated an I want to convert it to an array. However in some cases, I require integer parsing, sometimes double. Is there a way I can pass mapToDouble or mapToInt rather than writing the entire 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 make this into a function, where I can have a generic function and make store the appropriate array?
You can do it with a simple function that accepts String and a Function<String, T> that will convert every string element using this function. The good news is that this function may return any type you want: Integer, Double, BigDecimal, String or any other type you want. In below example I use a method reference like:
Integer::valueOf to convert elements to Integer values Double::valueOf to convert elements to Double valuesString::valueOf to convert elements to String valuesConsider 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 following example is:
[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]
I hope it helps.
There is a way, but you wouldn't be working with primitives any more, because generics in Java doesn't support primitive types. Instead, you can use Integer and Double wrapper types:
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 as follows:
Integer[] ints = convert("1, ,3", Integer::parseInt, true, -1, 0, Integer[]::new);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With