I want to convert List<String>
to List<Object>
.
One of the existing methods is returning List<String>
and I want to convert it to List<Object>
. Is there a direct way in Java other then iterating over and converting element by element?
Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object . The constructor takes a Collection , but List is a subinterface of Collection , so you can just use the List<String> . Show activity on this post.
Yes. ArrayList has a toArray() method.
Pass the List<String>
as a parameter to the constructor of a new ArrayList<Object>
.
List<Object> objectList = new ArrayList<Object>(stringList);
Any Collection
can be passed as an argument to the constructor as long as its type extends the type of the ArrayList
, as String
extends Object
. The constructor takes a Collection
, but List
is a subinterface of Collection
, so you can just use the List<String>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With