Good afternoon, Community!
I have a List:
List<String> rate = new ArrayList<>();
and I need to convert the data into a float if it can be done with java 8 stream. I was doing it in the following way:
float valueRate = Float.parseFloat(rate);
Good afternoon, Community!
I have a List:
List<String> rate = new ArrayList<>();
and I need to convert the data into a float if it can be done with java 8 stream. I was doing it in the following way:
float valueRate = Float.parseFloat(rate);
Try it like this:
float using Float.valueOf()List<String> list = List.of("1.2", "3.4", "2.5f");
List<Float> floats = list.stream().map(Float::valueOf).collect(Collectors.toList());
System.out.println(floats);
Prints
[1.2, 3.4, 2.5]
You can use Stream#map with Float.valueOf (to avoid autoboxing).
List<Float> res = rate.stream().map(Float::valueOf).collect(Collectors.toList());
With Java 16:
List<Float> res = rate.stream().map(Float::valueOf).toList();
Stream::toList available since Java-16.