To convert a list of strings to a list of floats in Java, you can use the Float.parseFloat()
method to parse each string as a float.
In this example,
stringList
.Float.parseFloat()
method to parse it as a float.import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> stringList = List.of("1.1", "2.2", "3.3", "4.4", "5.5");
List<Float> floatList = new ArrayList<>();
for (String s : stringList) {
floatList.add(Float.parseFloat(s));
}
System.out.println(floatList);
}
}
[1.1, 2.2, 3.3, 4.4, 5.5]
In this tutorial, we learned How to Convert a List of Strings to a List of Floats in Java language with well detailed examples.