How to Convert a Set of Floats to a Set of Strings in Java - Step by Step Examples
How to Convert a Set of Floats to a Set of Strings in Java ?
Answer
To convert a set of floats to a set of strings in Java, you can use streams to map each float to its string representation and collect the results into a new set.
✐ Examples
1 Convert Set of Floats to Set of Strings Example 1
In this example,
- First, we create a set named
floatSet
using theSet.of
method, which contains the float values1.1f
,2.2f
, and3.3f
. This is done using theFloat
class to box the primitive float values. - Next, we use the
stream
method onfloatSet
to create a stream of the set's elements. - We then apply the
map
function to the stream. Themap
function takes a lambda expressionf -> f.toString()
that converts each float to its string representation. - The
collect
method is then used to gather the results of themap
operation into a new set. We useCollectors.toSet()
to achieve this. - The resulting set of strings is stored in a variable named
stringSet
. - Finally, we print the value of
stringSet
to standard output usingSystem.out.println
.
Java Program
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Set<Float> floatSet = Set.of(1.1f, 2.2f, 3.3f);
Set<String> stringSet = floatSet.stream()
.map(f -> f.toString())
.collect(Collectors.toSet());
System.out.println("Set of strings: " + stringSet);
}
}
Output
Set of strings: [1.1, 2.2, 3.3]
2 Convert Set of Floats to Set of Strings Example 2
In this example,
- We start by defining a set named
floatSet2
using theSet.of
method, which contains the float values4.4f
,5.5f
, and6.6f
. - We then create a stream from
floatSet2
by calling thestream
method on it. - Using the
map
function, we convert each float in the stream to its string representation using the lambda expressionf -> f.toString()
. - The
collect
method is used to gather the results into a set by usingCollectors.toSet()
. - The resulting set of strings is stored in a variable called
stringSet2
. - Finally, we print the value of
stringSet2
to standard output usingSystem.out.println
.
Java Program
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Set<Float> floatSet2 = Set.of(4.4f, 5.5f, 6.6f);
Set<String> stringSet2 = floatSet2.stream()
.map(f -> f.toString())
.collect(Collectors.toSet());
System.out.println("Set of strings: " + stringSet2);
}
}
Output
Set of strings: [4.4, 5.5, 6.6]
Summary
In this tutorial, we learned How to Convert a Set of Floats to a Set of Strings 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 ?