'How to convert an ArrayList<Integer> to ArrayList<String>? [closed]
I have to convert an Int arraylist to String Arraylist so JText in swing can print the numbers of the String ArrayList.
private static ArrayList<Integer> numen = new ArrayList<Integer>
((Collections.nCopies(49,0)));
private static ArrayList<String> numens = new ArrayList<String>
((Collections.nCopies(49, "0")));
for (int myInt : numen){ numens.add(String.valueOf(myInt)); }
Solution 1:[1]
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<String> stringVals = numbers.stream().map(a -> a.toString()).collect(Collectors.toList());
stringVals.stream().forEach(System.out::println);
Solution 2:[2]
The code you are using for the conversion is fine:
ArrayList<String> numens = new ArrayList<String>();
for (int myInt : numen) {
numens.add(String.valueOf(myInt));
}
As explained, in @Ryan's answer, you could also do this using Java 8+ streams. But (IMO) that is not a significant improvement.
Note: if you are looking for a way to do this without creating a new list, that is not possible in Java.
But the thing that puzzles me is your rationalization for doing this:
I have to convert an
IntegerArraylisttoStringArraylistsoJTextin Swing can print the numbers of theStringArrayList.
Firstly, there is no JText type. There is a JTextField or a JTextArea.
Secondly, neither of these class accept a List to populate the field or area.
So ...
If you are just populating a single field / area from a list, you are going to have to convert the list to a string. And if you are doing that, you don't need to convert an ArrayList<Integer> to an ArrayList<String> to do it. For example, just call toString() on the ArrayList<Integer> will format it so that it can be inserted into a JTextField or JTextArea.
If you are just populating a list or array of fields / areas from the list, you are going to have to iterate the list to do it. At that point, you may as well iterate the original ArrayList<Integer>.
In short, your question appears to be an XY problem.
Solution 3:[3]
toString
If your goal is a textual representation of the integers, just call AbstractCollection#toString on your original ArrayList< Integer >. No need to create another list.
List< Integer > integers = List.of( 1 , 2 , 3 ) ;
String output = integers.toString() ;
See this code run live at Ideone.com.
[1, 2, 3]
Stream#map
If your goal is generating text, use a stream to generate text with newlines. Calling Stream#map changes our stream of Integer objects into a stream of String objects. We combine those with a StringBuilder, producing a final multi-line String object at the end.
List < Integer > integers = List.of( 1 , 2 , 3 );
String s =
integers
.stream()
.map( integer -> integer.toString() + "\n" )
.collect( StringBuilder :: new , StringBuilder :: append , StringBuilder :: append )
.toString();
1
2
3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ryan |
| Solution 2 | |
| Solution 3 |
