java java获取hashmap键作为整数数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7412597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:51:31  来源:igfitidea点击:

java Get hashmap keys as integer array

javaintegerhashmap

提问by Krishnabhadra

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

这里我的 HashMap 的关键是以秒为单位的时间作为字符串,即我像这样向 hashmap 添加值

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?

有没有办法做到这一点?

回答by dacwe

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

没有将Strings列表转换为s列表的直接方法Integer

  1. Either you need to redefine your valueHashMaplike 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 Longas 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 Longinstead).

  1. 要么你需要valueHashMap像这样重新定义你的:

    public HashMap<Integer, People> valueHashMap  = new HashMap<Integer, People>();
    
    ....
    
    ArrayList<Integer> intKeys = new ArrayList<Integer>(valueHashMap.keySet());
    
  2. 或者你需要循环:

    ArrayList<Integer> intKeys = new ArraList<Integer>();
    
    for (String stringKey : valueHashMap.keySet())
         intKeys.add(Integer.parseInt(stringKey);
    
  3. 但是,我建议您改用Longas 键

    public HashMap<Long, People> valueHashMap  = new HashMap<Long, People>();
    

    那么就不会转换为int(并且你可以使用上面的 (1) with Long)

回答by pablochan

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.

您不能将一种类型的 List 转换为另一种类型的 List,因此您必须遍历键并解析每个键。

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

回答by JB Nizet

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:

你真的有类型问题。为什么要将 longs 更改为 Strings 以将它们存储在 map 中。为什么不简单地使用 Long,它需要更少的内存并且更具描述性。那为什么要用Integer.toString 把long 转成String 呢?通过将 long 转换为 int,您可能会丢失信息。代码应该如下所示:

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());

回答by Pokuri

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

您可以org.apache.commons.collections.Transformer按如下方式使用类。

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