I have a hashmap like this
public HashMap <String,People> valueHashMap = new Hashmap();
Here the key to my HashMap is time in seconds as string, ie I am adding value to hashmap like this
long timeSinceEpoch = System.currentTimeMillis()/1000;
valueHashMap.put(
Integer.toString((int)timeSinceEpoch)
, people_obj
);
Now I want to get all keys in the hashmap into an array list of integer.
ArrayList<Integer> intKeys = valueHashMap.keys()...
Is there any way to do that?
There is no direct way of converting a list of Strings to a list of Integers:
Either you need to redefine your valueHashMap like this:
public HashMap<Integer, People> valueHashMap = new HashMap<Integer, People>();
....
ArrayList<Integer> intKeys = new ArrayList<Integer>(valueHashMap.keySet());
Or you need to loop:
ArrayList<Integer> intKeys = new ArraList<Integer>();
for (String stringKey : valueHashMap.keySet())
intKeys.add(Integer.parseInt(stringKey);
I would advice you however to use the Long as key instead:
public HashMap<Long, People> valueHashMap = new HashMap<Long, People>();
then there would be no casting to int (and you can use (1) above with Long instead).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With