如何转换列表< String>列出<对象>
我想将 List< String>
转换为 List< Object>
。
现有方法之一是返回 List< String>
,我想将它转换为 List< Object>
。
在Java中有直接的方法,然后迭代并按元素转换元素?
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?
传递 List< String>
作为新的 ArrayList< Object>的构造函数的参数
。
Pass the List<String>
as a parameter to the constructor of a new ArrayList<Object>
.
List<Object> objectList = new ArrayList<Object>(stringList);
任何集合
都可以作为参数传递只要它的类型扩展了 ArrayList
类型,就像 String $ c> extends
对象
。构造函数使用 Collection
,但 List $ c>是
Collection $ c $的子接口所以你可以使用
List< String>
。
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>
.