如何转换列表< String>列出<对象> [英] How to Convert List<String> to List<Object>

查看:155
本文介绍了如何转换列表< 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 extends 对象。构造函数使用 Collection ,但 List 是 Collection 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>.

这篇关于如何转换列表&lt; String&gt;列出&lt;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆