将HashMap转换为ArrayList [英] Converting HashMap to ArrayList

查看:748
本文介绍了将HashMap转换为ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  HashMap< String,ExceptionLifeCycleDataBean>< p> transitionHash = new HashMap< String,ExceptionLifeCycleDataBean>(); 

ArrayList< ExceptionLifeCycleDataBean> cardholderDataRecords = new ArrayList< ExceptionLifeCycleDataBean>();

我做的是

  cardholderDataRecords.add((ExceptionLifeCycleDataBean)transitionHash.values()); 

投掷

  java.lang.ClassCastException:java.util.HashMap $ Values不能转换为com.reportss.bean.ExceptionLifeCycleDataBean 


解决方案

您试图将值集合转换为单个 ExceptionLifeCycleDataBean

你可以很容易的得到这个列表:

 列表与LT; ExceptionLifeCycleDataBean> beans = 
new ArrayList< ExceptionLifeCycleDataBean>(transitionHash.values());

或者添加到现有集合中:

  cardholderDataRecords.addAll(transitionHash.values()); 

不需要转换。

I just want to move the transitionHash map values into the cardholderDataRecords arraylist.

HashMap<String,ExceptionLifeCycleDataBean> transitionHash = new HashMap<String,ExceptionLifeCycleDataBean>();

ArrayList<ExceptionLifeCycleDataBean> cardholderDataRecords = new ArrayList<ExceptionLifeCycleDataBean>();

i am doing as

cardholderDataRecords.add((ExceptionLifeCycleDataBean) transitionHash.values());

It's throwing

java.lang.ClassCastException: java.util.HashMap$Values cannot be cast to com.reportss.bean.ExceptionLifeCycleDataBean

解决方案

You're trying to cast the collection of values to a single ExceptionLifeCycleDataBean.

You can very easily get the list though:

List<ExceptionLifeCycleDataBean> beans =
    new ArrayList<ExceptionLifeCycleDataBean>(transitionHash.values());

Or to add to an existing collection, with:

cardholderDataRecords.addAll(transitionHash.values());

No casts necessary.

这篇关于将HashMap转换为ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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