Home > Enterprise >  How to convert an ArrayList<Integer> to ArrayList<String>?
How to convert an ArrayList<Integer> to ArrayList<String>?

Time:05-07

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)); }

CodePudding user response:

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);

CodePudding user response:

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]

  • Related