How to Convert a Set of Strings to a Set of Floats in Java - Step by Step Examples
How to Convert a Set of Strings to a Set of Floats in Java ?
Answer
To convert a set of strings to a set of floats in Java, you can use the Stream
API along with the map
and filter
functions to transform each string value into its corresponding float value.
✐ Examples
1 Convert a Set of Strings {"1.1", "2.2", "3.3"} to Floats
In this example,
- First, we create a set of strings named
stringSet1
with values{"1.1", "2.2", "3.3"}
. - Next, we use the
Stream
API to convert each string value instringSet1
to its corresponding float value. We use themap
function to apply a lambda expression that converts each string to a float usingFloat.parseFloat()
. - We use the
filter
function to remove anynull
values that may occur during the conversion process. - Finally, we collect the resulting stream of floats into a set using the
Collectors.toSet()
method.
Java Program
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Set<String> stringSet1 = Set.of("1.1", "2.2", "3.3");
Set<Float> floatSet1 = stringSet1.stream()
.map(Float::parseFloat)
.filter(f -> f != null)
.collect(Collectors.toSet());
System.out.println(floatSet1);
}
}
Output
[1.1, 2.2, 3.3]
2 Convert a Set of Strings {"4.5", "5.6", "6.7"} to Floats
In this example,
- First, we create a set of strings named
stringSet2
with values{"4.5", "5.6", "6.7"}
. - Next, we use the
Stream
API to convert each string value instringSet2
to its corresponding float value. We use themap
function to apply a lambda expression that converts each string to a float usingFloat.parseFloat()
. - We use the
filter
function to remove anynull
values that may occur during the conversion process. - Finally, we collect the resulting stream of floats into a set using the
Collectors.toSet()
method.
Java Program
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Set<String> stringSet2 = Set.of("4.5", "5.6", "6.7");
Set<Float> floatSet2 = stringSet2.stream()
.map(Float::parseFloat)
.filter(f -> f != null)
.collect(Collectors.toSet());
System.out.println(floatSet2);
}
}
Output
[4.5, 5.6, 6.7]
Summary
In this tutorial, we learned How to Convert a Set of Strings to a Set of Floats in Java language with well detailed examples.
More Java Sets Tutorials
- How to create an Empty Set in Java ?
- How to Initialize a Set in Java ?
- How to Get Length of a Set in Java ?
- How to create a Set of size N in Java ?
- How to create a Set of Numbers from 1 to N in Java ?
- How to create a Set of integers in Java ?
- How to create a Set of Strings in Java ?
- How to Access Items in a Set in Java ?
- How to get a Random Item in a Set in Java ?
- How to Iterate Over a Set in Java ?
- How to check if a Set is Empty in Java ?
- How to check if a Set is Not Empty in Java ?
- How to get Subset from a Set in Java ?
- How to check if a Specific Item is present in the Set in Java ?
- How to check if a Set contains all the items of Another Set in Java ?
- How to Sort Items of a Set in Java ?
- How to Copy a Set in Java ?
- How to add an Item to a Set in Java ?
- How to find Union of Two Sets in Java ?
- How to find Intersection of Two Sets in Java ?
- How to check if Two Sets are Equal in Java ?
- How to Convert a Set of Integers to a Set of Strings in Java ?
- How to Convert a Set of Strings to a Set of Integers in Java ?
- How to Convert a Set of Floats to a Set of Strings in Java ?
- How to Convert a Set of Strings to a Set of Floats in Java ?
- How to Filter Items of a Set based on a Condition in Java ?
- How to Remove Specific Item from a Set in Java ?
- How to Remove Items from Set based on a Condition in Java ?
- How to create a Set of Sets in Java ?