0

I have my hash map of type:

private HashMap<String, HashMapObject> likesYearHashMap;

where HashMapObject :

public class HashMapObject {
private int count;
private String id;
public HashMapObject(int count, String id) {
 super();
 this.count = count;
 this.id = id;
}

}

how to convert values into my HashMap to HashMapObject [] array?

I get error of cast getting likesYearHashMap.values();

asked Jan 13, 2014 at 15:25
1
  • 2
    Map#values() returns a Collection. Iterate over it and add its element to your array. Commented Jan 13, 2014 at 15:26

3 Answers 3

2

Like this:

map.values().toArray(new HashMapObject[map.size()])
answered Jan 13, 2014 at 15:27

Comments

1
likesYearHashMap.values().toArray (new HashMapObject[likesYearHashMap.size ()])
answered Jan 13, 2014 at 15:28

Comments

0

Thanks to @Vakh and @NinChimpsky i post the complete solution:

To convert into array :

HashMapObject[] array = new HashMapObject[likesYearHashMap.size()];
likesYearHashMap.values().toArray(array);

To convert to ArrayList

Collection<HashMapObject> values = likesYearHashMap.values();
ArrayList<HashMapObject> arrayList= new ArrayList<HashMapObject>(values);
answered Jan 13, 2014 at 15:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.