Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List conversion between different data types

Tags:

java

What would be the simplest way to convert List<Integer> to List<String> objects. Currently I iterate through all the integers and add them to a List<String> objects, is there a utility function which is already available?

like image 392
user339108 Avatar asked Dec 28 '22 04:12

user339108


2 Answers

You found a simple way. The type are "incompatible", so you need a conversion rule anyway (which is simple in your case). The algorithm will always be O(n), regardless of iterating through the collections manually or calling some API method from some 3rd party library (JRE doesn't offer API for your task).

like image 139
Andreas Dolk Avatar answered Jan 08 '23 00:01

Andreas Dolk


While there are some libraries that can do general List transformations (like Google's Guava) using one for this case probably won't save you any lines of code as you'd need to create a Function object, and that alone involves so much boilerplate that your existing code is probably smaller.

like image 37
Laurence Gonsalves Avatar answered Jan 08 '23 00:01

Laurence Gonsalves