Get keys from HashMap in Java

后端 未结 14 2638
野的像风
野的像风 2020-12-04 07:03

I have a Hashmap in Java like this:

private Map team1 = new HashMap();

Then I fill it like th

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 07:37

    A solution can be, if you know the key position, convert the keys into an String array and return the value in the position:

    public String getKey(int pos, Map map) {
        String[] keys = (String[]) map.keySet().toArray(new String[0]);
    
        return keys[pos];
    }
    

提交回复
热议问题