Convert Set<Integer> to Set<String> in Java
23,799
Solution 1
No. You have to format each integer and add it to your string set.
Solution 2
No. The best way is a loop.
HashSet<String> strs = new HashSet<String>(ints.size());
for(Integer integer : ints) {
strs.add(integer.toString());
}
Something simple and relatively quick that is straightforward and expressive is probably best.
(Update:) In Java 8, the same thing can be done with a lambda expression if you'd like to hide the loop.
HashSet<String> strs = new HashSet<>(ints.size());
ints.forEach(i -> strs.add(i.toString()));
or, using Streams,
Set<String> strs = ints.stream().map(Integer::toString).collect(toSet());
Solution 3
use Java8 stream map and collect abilities:
Set< String > stringSet =
intSet.stream().map(e -> String.valueOf(e)).collect(Collectors.toSet());
Solution 4
You could use Commons Collections' TransformedSet or Guava's Collections2.transform(...)
In both cases, your functor would presumably simply call the Integer's toString().
Solution 5
You can use a decorator if you really don't want to iterate through the entire set
Author by
MatBanik
Updated on December 28, 2020Comments
-
MatBanik over 3 years
Is there a simple way to convert
Set<Integer>
toSet<String>
without iterating through the entire set? -
Cajunluke almost 13 yearsCan you provide an example? I'm not familiar with the concept of decorators.
-
Oliver Charlesworth almost 13 yearsHow would you apply the decorator pattern in this scenario?
-
Vishy almost 13 yearsPerhaps you meant
toString
instead ofstringValue
;) -
Kathy Van Stone almost 13 yearsIf we just wanted a Set<Integer> to look like a Set<String> but not create another set (say if only a fraction of the set is likely to be checked) you would create a subclass of AbstractSet<String> that would wrap the Set<Integer> and do the conversions in individual methods.
-
Cajunluke almost 13 years@Peter That indeed I did. I must have been thinking of
Integer
's.intValue()
. -
VirtualTroll almost 13 yearsWell it would depends on Mat needs. For a single need (function), i will probably create a simple object containing the set and implementing my required function. For more than one need, I will probably choose another solution :)
-
Luke Hutteman almost 13 yearsTheoretically, your code could throw a
NullPointerException
in the call totoString()
as aSet<T>
can contain a null value. -
Isaac Truett almost 13 yearsPresumably, someone disagreed with the "no" answers and feels that a wrapper/decorator is a better answer.
-
Cajunluke almost 13 years@Luke True, but, as this is an example, handling it would be unimportant and, honestly, I wouldn't handle it even in production code until it proved to be an issue. Why bother if you know the input set won't have any nulls?
-
Vishy almost 13 years@Luke, For this reason I prefer the shorter
""+integer
;) -
Luke Hutteman almost 13 years@CajunLuke: I agree that if you know the input Set won't have any nulls, there's no need to bother with the null-check. The question does not mention whether nulls are possible though, and online code-samples do sometimes end up copied-and-pasted into production code (where I absolutely disagree with your approach of waiting "until it proved to be an issue").
-
Luke Hutteman almost 13 years@Peter: that maps nulls to a
String
instance of"null"
though, instead of the probably more accurate actual null value you would get with the longerinteger == null ? null : integer.toString()
. -
Vishy almost 13 years@Luke, true, Another way to map null Integer is to an empty string, which works for loading text tables into a spreadsheet.
-
Cajunluke almost 9 years@PeterLawrey Instead of dealing with a much of extra method calls and a new StringBuilder,
String.valueOf(integer)
is faster and has the same result as""+integer
. -
Vishy almost 9 years@CajunLuke While a little faster, it is more work for the developer. If you concerned about speed you wouldn't be adding Integer as a String to HashSet. You might be using a library which wraps an
int[]
to save theint
as a primitive. -
Cajunluke almost 9 years@PeterLawrey I never thought of wrapping an int[] to keep the primitive. I'm going to have to keep that in mind.