<p>I have a list of integers, <code>List&lt;Integer&gt;</code> and I'd like to convert all the integer objects into Strings, thus finishing up with a new <code>List&lt;String&gt;</code>.</p>

<p>Naturally, I could create a new <code>List&lt;String&gt;</code> and loop through the list calling <code>String.valueOf()</code> for each integer, but I was wondering if there was a better (read: <em>more automatic</em>) way of doing it?</p>

[{'answer_id': 18529, 'author': 'jsight', 'author_id': 1432, 'author_profile': 'https://Stackoverflow.com/users/1432', 'pm_score': 7, 'selected': True, 'text': "<p>As far as I know, iterate and instantiate is the only way to do this.  Something like (for others potential help, since I'm sure you know how to do this):</p>\n\n<pre><code>List&lt;Integer&gt; oldList = ...\n/* Specify the size of the list up front to prevent resizing. */\nList&lt;String&gt; newList = new ArrayList&lt;&gt;(oldList.size());\nfor (Integer myInt : oldList) { \n  newList.add(String.valueOf(myInt)); \n}\n</code></pre>\n"}, {'answer_id': 18547, 'author': 'jsight', 'author_id': 1432, 'author_profile': 'https://Stackoverflow.com/users/1432', 'pm_score': 2, 'selected': False, 'text': '<p>@Jonathan: I could be mistaken, but I believe that String.valueOf() in this case will call the String.valueOf(Object) function rather than getting boxed to String.valueOf(int).  String.valueOf(Object) just returns "null" if it is null or calls Object.toString() if non-null, which shouldn\'t involve boxing (although obviously instantiating new string objects is involved).</p>\n'}, {'answer_id': 18558, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 3, 'selected': False, 'text': "<p>Instead of using String.valueOf I'd use .toString(); it avoids some of the auto boxing described by @johnathan.holland</p>\n\n<p>The javadoc says that valueOf returns the same thing as Integer.toString().</p>\n\n<pre><code>List&lt;Integer&gt; oldList = ...\nList&lt;String&gt; newList = new ArrayList&lt;String&gt;(oldList.size());\n\nfor (Integer myInt : oldList) { \n  newList.add(myInt.toString()); \n}\n</code></pre>\n"}, {'answer_id': 18579, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 2, 'selected': False, 'text': "<p>I think using Object.toString() for any purpose other than debugging is probably a really bad idea, even though in this case the two are functionally equivalent (assuming the list has no nulls).  Developers are free to change the behavior of any toString() method without any warning, including the toString() methods of any classes in the standard library.</p>\n\n<p>Don't even worry about the performance problems caused by the boxing/unboxing process.  If performance is critical, just use an array.  If it's really critical, don't use Java.  Trying to outsmart the JVM will only lead to heartache.</p>\n"}, {'answer_id': 18595, 'author': 'Mike Polen', 'author_id': 212, 'author_profile': 'https://Stackoverflow.com/users/212', 'pm_score': 3, 'selected': False, 'text': '<p>The source for String.valueOf shows this:</p>\n\n<pre><code>public static String valueOf(Object obj) {\n    return (obj == null) ? "null" : obj.toString();\n}\n</code></pre>\n\n<p>Not that it matters much, but I would use toString.</p>\n'}, {'answer_id': 18733, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 1, 'selected': False, 'text': '<p>You can\'t avoid the "boxing overhead"; Java\'s faux generic containers can only store Objects, so your ints must be boxed into Integers.  In principle it could avoid the downcast from Object to Integer (since it\'s pointless, because Object is good enough for both String.valueOf and Object.toString) but I don\'t know if the compiler is smart enough to do that.  The conversion from String to Object should be more or less a no-op, so I would be disinclined to worry about that one.</p>\n'}, {'answer_id': 18734, 'author': 'serg10', 'author_id': 1853, 'author_profile': 'https://Stackoverflow.com/users/1853', 'pm_score': 2, 'selected': False, 'text': '<p>Not core Java, and not generic-ified, but the popular Jakarta commons collections library has some useful abstractions for this sort of task.  Specifically, have a look at the collect methods on</p>\n\n<p><a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/CollectionUtils.html" rel="nofollow noreferrer">CollectionUtils</a></p>\n\n<p>Something to consider if you are already using commons collections in your project.</p>\n'}, {'answer_id': 19191, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 5, 'selected': False, 'text': '<p>What you\'re doing is fine, but if you feel the need to \'Java-it-up\' you could use a <a href="http://commons.apache.org/collections/api/org/apache/commons/collections/Transformer.html" rel="nofollow noreferrer">Transformer</a> and the <a href="http://commons.apache.org/collections/api/org/apache/commons/collections/CollectionUtils.html" rel="nofollow noreferrer">collect method</a> from <a href="http://commons.apache.org/" rel="nofollow noreferrer">Apache Commons</a>, e.g.:</p>\n\n<pre><code>public class IntegerToStringTransformer implements Transformer&lt;Integer, String&gt; {\n   public String transform(final Integer i) {\n      return (i == null ? null : i.toString());\n   }\n}\n</code></pre>\n\n<p>..and then..</p>\n\n<pre><code>CollectionUtils.collect(\n   collectionOfIntegers, \n   new IntegerToStringTransformer(), \n   newCollectionOfStrings);\n</code></pre>\n'}, {'answer_id': 33028, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 2, 'selected': False, 'text': '<p>To the people concerned about "boxing" in jsight\'s answer: there is none. <code>String.valueOf(Object)</code> is used here, and no unboxing to <code>int</code> is ever performed.</p>\n\n<p>Whether you use <code>Integer.toString()</code> or <code>String.valueOf(Object)</code> depends on how you want to handle possible nulls. Do you want to throw an exception (probably), or have "null" Strings in your list (maybe). If the former, do you want to throw a <code>NullPointerException</code> or some other type?</p>\n\n<p>Also, one small flaw in jsight\'s response: <code>List</code> is an interface, you can\'t use the new operator on it. I would probably use a <code>java.util.ArrayList</code> in this case, especially since we know up front how long the list is likely to be.</p>\n'}, {'answer_id': 57566, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 2, 'selected': False, 'text': '<p>An answer for experts only:</p>\n\n<pre><code>    List&lt;Integer&gt; ints = ...;\n    String all = new ArrayList&lt;Integer&gt;(ints).toString();\n    String[] split = all.substring(1, all.length()-1).split(", ");\n    List&lt;String&gt; strs = Arrays.asList(split);\n</code></pre>\n'}, {'answer_id': 61663, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 0, 'selected': False, 'text': '<p>Just for fun, a solution using the jsr166y fork-join framework that should in JDK7.</p>\n\n<pre><code>import java.util.concurrent.forkjoin.*;\n\nprivate final ForkJoinExecutor executor = new ForkJoinPool();\n...\nList&lt;Integer&gt; ints = ...;\nList&lt;String&gt; strs =\n    ParallelArray.create(ints.size(), Integer.class, executor)\n    .withMapping(new Ops.Op&lt;Integer,String&gt;() { public String op(Integer i) {\n        return String.valueOf(i);\n    }})\n    .all()\n    .asList();\n</code></pre>\n\n<p>(Disclaimer: Not compiled. Spec is not finalised. Etc.)</p>\n\n<p>Unlikely to be in JDK7 is a bit of type inference and syntactical sugar to make that withMapping call less verbose:</p>\n\n<pre><code>    .withMapping(#(Integer i) String.valueOf(i))\n</code></pre>\n'}, {'answer_id': 125829, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>This is such a basic thing to do I wouldn't use an external library (it will cause a dependency in your project that you probably don't need).</p>\n\n<p>We have a class of static methods specifically crafted to do these sort of jobs. Because the code for this is so simple we let Hotspot do the optimization for us. This seems to be a theme in my code recently: write very simple (straightforward) code and let Hotspot do its magic. We rarely have performance issues around code like this - when a new VM version comes along you get all the extra speed benefits etc.</p>\n\n<p>As much as I love Jakarta collections, they don't support Generics and use 1.4 as the LCD. I am wary of Google Collections because they are listed as Alpha support level!</p>\n"}, {'answer_id': 1227099, 'author': 'Ben Lings', 'author_id': 41012, 'author_profile': 'https://Stackoverflow.com/users/41012', 'pm_score': 7, 'selected': False, 'text': '<p>Using <a href="https://github.com/google/guava/" rel="noreferrer">Google Collections from Guava-Project</a>, you could use the <code>transform</code> method in the <a href="https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/Lists.html" rel="noreferrer">Lists</a> class</p>\n\n<pre><code>import com.google.common.collect.Lists;\nimport com.google.common.base.Functions\n\nList&lt;Integer&gt; integers = Arrays.asList(1, 2, 3, 4);\n\nList&lt;String&gt; strings = Lists.transform(integers, Functions.toStringFunction());\n</code></pre>\n\n<p>The <code>List</code> returned by <code>transform</code> is a <em>view</em> on the backing list - the transformation will be applied on each access to the transformed list.</p>\n\n<p>Be aware that <code>Functions.toStringFunction()</code> will throw a <code>NullPointerException</code> when applied to null, so only use it if you are sure your list will not contain null.</p>\n'}, {'answer_id': 2396243, 'author': 'Mario Fusco', 'author_id': 112779, 'author_profile': 'https://Stackoverflow.com/users/112779', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://code.google.com/p/lambdaj/" rel="nofollow noreferrer">Lambdaj</a> allows to do that in a very simple and readable way. For example, supposing you have a list of Integer and you want to convert them in the corresponding String representation you could write something like that;</p>\n\n<pre><code>List&lt;Integer&gt; ints = asList(1, 2, 3, 4);\nIterator&lt;String&gt; stringIterator = convertIterator(ints, new Converter&lt;Integer, String&gt; {\n    public String convert(Integer i) { return Integer.toString(i); }\n}\n</code></pre>\n\n<p>Lambdaj applies the conversion function only while you\'re iterating on the result.</p>\n'}, {'answer_id': 5781802, 'author': 'Garrett Hall', 'author_id': 554988, 'author_profile': 'https://Stackoverflow.com/users/554988', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s a one-liner solution without cheating with a non-JDK library.</p>\n\n<pre><code>List&lt;String&gt; strings = Arrays.asList(list.toString().replaceAll("\\\\[(.*)\\\\]", "$1").split(", "));\n</code></pre>\n'}, {'answer_id': 12322903, 'author': 'Rodney P. Barbati', 'author_id': 1588303, 'author_profile': 'https://Stackoverflow.com/users/1588303', 'pm_score': -1, 'selected': False, 'text': '<p>I just wanted to chime in with an object oriented solution to the problem.</p>\n<p>If you model domain objects, then the solution is in the domain objects.  The domain here is a List of integers for which we want string values.</p>\n<p>The easiest way would be to not convert the list at all.</p>\n<p>That being said, in order to convert without converting, change the original list of Integer to List of Value, where Value looks something like this...</p>\n<pre><code>class Value {\n    Integer value;\n    public Integer getInt()\n    {\n       return value;\n    }\n    public String getString()\n    {\n       return String.valueOf(value);\n    }\n}\n</code></pre>\n<p>This will be faster and take up less memory than copying the List.</p>\n'}, {'answer_id': 23024375, 'author': 'Hakanai', 'author_id': 138513, 'author_profile': 'https://Stackoverflow.com/users/138513', 'pm_score': 7, 'selected': False, 'text': '<p>Solution for Java 8. A bit longer than the Guava one, but at least you don\'t have to install a library.</p>\n<pre class="lang-java prettyprint-override"><code>import java.util.Arrays;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\n//...\n\nList&lt;Integer&gt; integers = Arrays.asList(1, 2, 3, 4);\nList&lt;String&gt; strings = integers.stream().map(Object::toString)\n                                        .collect(Collectors.toList());\n</code></pre>\n<p>For Java 11,</p>\n<pre class="lang-java prettyprint-override"><code>List&lt;String&gt; strings = integers.stream().map(Object::toString)\n                                        .collect(Collectors.toUnmodifiableList());\n</code></pre>\n<p>Still no <code>map</code> convenience method, really?</p>\n'}, {'answer_id': 23293398, 'author': 'sandrozbinden', 'author_id': 1039517, 'author_profile': 'https://Stackoverflow.com/users/1039517', 'pm_score': 3, 'selected': False, 'text': '<p>Another Solution using Guava and Java 8</p>\n\n<pre><code>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5);\nList&lt;String&gt; strings = Lists.transform(numbers, number -&gt; String.valueOf(number));\n</code></pre>\n'}, {'answer_id': 45944083, 'author': 'nagendra547', 'author_id': 7438973, 'author_profile': 'https://Stackoverflow.com/users/7438973', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>I didn't see any solution which is following the principal of space\n  complexity. If list of integers has large number of elements then it's\n  big problem.</p>\n</blockquote>\n\n<pre><code>It will be really good to remove the integer from the List&lt;Integer&gt; and free\nthe space, once it's added to List&lt;String&gt;.\n</code></pre>\n\n<p>We can use iterator to achieve the same. </p>\n\n<pre><code>    List&lt;Integer&gt; oldList = new ArrayList&lt;&gt;();\n    oldList.add(12);\n    oldList.add(14);\n    .......\n    .......\n\n    List&lt;String&gt; newList = new ArrayList&lt;String&gt;(oldList.size());\n    Iterator&lt;Integer&gt; itr = oldList.iterator();\n    while(itr.hasNext()){\n        newList.add(itr.next().toString());\n        itr.remove();\n    }\n</code></pre>\n"}, {'answer_id': 56666320, 'author': 'Mahesh Yadav', 'author_id': 8350518, 'author_profile': 'https://Stackoverflow.com/users/8350518', 'pm_score': 2, 'selected': False, 'text': '<pre><code>List&lt;String&gt; stringList = integerList.stream().map((Object s)-&gt;String.valueOf(s)).collect(Collectors.toList())\n</code></pre>\n'}, {'answer_id': 61236486, 'author': 'Solubris', 'author_id': 1505487, 'author_profile': 'https://Stackoverflow.com/users/1505487', 'pm_score': 2, 'selected': False, 'text': '<p>A slightly more concise solution using the forEach method on the original list:</p>\n\n<pre><code>    List&lt;Integer&gt; oldList = Arrays.asList(1, 2, 3, 4, 5);\n    List&lt;String&gt; newList = new ArrayList&lt;&gt;(oldList.size());\n    oldList.forEach(e -&gt; newList.add(String.valueOf(e)));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/18524', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/916/']