2

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?

6
  • 2
    Why are you converting int to String before putting it in the Map? Commented Sep 14, 2011 at 7:22
  • @Sanjay T. Sharma can java hashmap take integer keys? Commented Sep 14, 2011 at 7:24
  • 1
    Yes if you define it as Map<Integer, People>. Commented Sep 14, 2011 at 7:25
  • 1
    It can take Integer keys (but not int keys). Commented Sep 14, 2011 at 7:26
  • 1
    Yes, integers are perfectly fine for being used as keys in a Map. It's just that you will be putting an Integer instead of the primitive int in the Map but I don't think that should be a problem for you. Also, auto-boxing will help you cut down the conversion from int to Integer call. Commented Sep 14, 2011 at 7:26

4 Answers 4

11

There is no direct way of converting a list of Strings to a list of Integers:

  1. 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());
    
  2. Or you need to loop:

    ArrayList<Integer> intKeys = new ArraList<Integer>();
    
    for (String stringKey : valueHashMap.keySet())
         intKeys.add(Integer.parseInt(stringKey);
    
  3. 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).

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

2 Comments

+1 using Long will help avoid the Year 2038 Problem.
There are some mistakes in option 2
1

You can't cast a List of one type to a List of another type, so you have to iterate through the keys and parse each one.

for(String k : valueHashMap.keySet()) {
    intKeys.add(Integer.valueOf(k));
}

3 Comments

That's parsing and not casting!
@Joachim Sauer: That's because my answer covered the general case (where you can sometimes cast) and my code was for this specific instance of the problem. Fixed it anyway to avoid confusion.
the problem is that many people call any kind of type conversion "casting" and that is wrong and confuses people even further.
0

You really have type problems. Why do you change the longs into Strings to store them in a map. Why not simply use Long, which needs less memory and is more descriptive. Then why use Integer.toString to transform a long into a String? By casting your long to an int, you risk loosing information by. Here's how the code should probably look like:

private Map<Long, People> valueHashMap = new Hashmap<Long, People>();

long timeSinceEpoch = System.currentTimeMillis()/1000;
valueHashMap.put(timeSinceEpoch, people_obj);
List<Long> longKeys = new ArrayList<Long>(valueHashMap.keySet());

Comments

0

You can use org.apache.commons.collections.Transformer class for that as follows.

List<Integer> intKeys  = (List<Integer>)CollectionUtils.collect(valueHashMap.keySet(), new Transformer() {
                                    @Override
                                    public Object transform(Object key) {
                                        return Integer.valueOf(key);
                                    }
                                }, new ArrayList<Integer>());

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.