Get keys from HashMap in Java

后端 未结 14 2633
野的像风
野的像风 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:59

    A HashMap contains more than one key. You can use keySet() to get the set of all keys.

    team1.put("foo", 1);
    team1.put("bar", 2);
    

    will store 1 with key "foo" and 2 with key "bar". To iterate over all the keys:

    for ( String key : team1.keySet() ) {
        System.out.println( key );
    }
    

    will print "foo" and "bar".

提交回复
热议问题