Get keys from HashMap in Java

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

    What I'll do which is very simple but waste memory is to map the values with a key and do the oposite to map the keys with a value making this:

    private Map team1 = new HashMap();

    it's important that you use so you can map keys:Value and Value:Keys like this

    team1.put("United", 5);

    team1.put(5, "United");

    So if you use team1.get("United") = 5 and team1.get(5) = "United"

    But if you use some specific method on one of the objects in the pairs I'll be better if you make another map:

    private Map team1 = new HashMap();

    private Map team1Keys = new HashMap();

    and then

    team1.put("United", 5);

    team1Keys.put(5, "United");

    and remember, keep it simple ;)

提交回复
热议问题