Mdhar9e Mdhar9e -4 years ago 181
Java Question

Converting HashMap to ArrayList

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download