48

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

2 Answers 2

130

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I successfully converted from HashMap to ArrayList using addAll. But i can't loop the new arraylist using : for (ExceptionLifeCycleDataBean bean : cardholderDataRecords) can it be done ?
@user2396640 I have no idea what you mean by that. Maybe you should ask a new question with details.
-3

Converting Data HashSet to Array List

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

Same Way, You can convert ArrayList to Hashmap.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.