将无类型的Arraylist转换为类型化的Arraylist

Amo*_*eba 4 java arraylist

是否有更优雅的解决方案将' Arraylist'变成' Arraylist<Type>'?

当前代码:

ArrayList productsArrayList=getProductsList();
ArrayList<ProductListBean> productList = new ArrayList<ProductListBean>();

for (Object item : productsArrayList)
{
  ProductListBean product = (ProductListBean)item;
  productList.add(product);
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*hen 10

ArrayList<ProductListBean> productList = (ArrayList<ProductListBean>)getProductsList();
Run Code Online (Sandbox Code Playgroud)

请注意,这本质上是不安全的,因此只有在无法更新getProductsList时才应使用它.