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
3 Answers 3
Like this:
map.values().toArray(new HashMapObject[map.size()])
answered Jan 13, 2014 at 15:27
Comments
likesYearHashMap.values().toArray (new HashMapObject[likesYearHashMap.size ()])
answered Jan 13, 2014 at 15:28
Comments
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
lang-java
Map#values()
returns aCollection
. Iterate over it and add its element to your array.