如何使自定义密钥类型使Java HashMap正常工作? [英] How to make java HashMap work properly with custom Key type?

查看:158
本文介绍了如何使自定义密钥类型使Java HashMap正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的问题很简单,但是我找不到解决方案,所以我决定在这里问。我需要的是使用像这样的自定义键类型来创建HashMap:

  HashMap< Pair< Integer,Integer>,StrategyPoint> ; myMap = new HashMap< Pair< Integer,Integer>,StrategyPoint> (); 

然而我在这里错过了一些东西,因为HashMap停止正常工作。首先,密钥变得不唯一,并且可以在密钥集中找到具有相同值的Pair的不同实例。此外,包含关键功能不会以我想它的方式工作:)。我显然错过了一些东西,更有可能我应该以某种方式定义一种方法来比较我的Pair类中的实例。不过,我尝试在我的Pair类中实现与compareTo比较,它仍然不起作用。任何建议吗?



我的原始代码有点杂乱和不友好的阅读,所以我举了一个例子来说明我的问题。这里是代码:

  HashMap< Pair< Integer,Integer>,StrategyPoint> myMap = new HashMap< Pair< Integer,Integer>,StrategyPoint> (); 
对<整数,整数> myPair =新对<整数,整数>(2,2);
StrategyPoint myPoint = new StrategyPoint(2,2,5,5,false);
myMap.put(myPair,myPoint);


对<整数,整数>搜索者=新对<整数,整数> (0,0);
searcher.setFirst(2);
searcher.setSecond(2);
System.out.println(myMap.containsKey(searcher));
System.out.println(myMap.containsKey(myPair));

执行结果为:

false

true

我调试过它,搜索器实例正在被正确填充,但是它似乎是HashMap拒绝在它的KeySet中找到它。



感谢您的帮助!

解决方案

您必须在 Pair 上正确实现 equals hashCode c $ c> class。


$ b

HashMap 使用这些方法来区分和散列关键类。


I think my question is quite simple, however i couldn't find the solution so I decided to ask here. What i need is to make a HashMap with a custom Key type like this:

HashMap<Pair<Integer, Integer>, StrategyPoint> myMap = new HashMap<Pair<Integer, Integer>, StrategyPoint> ();

However I am missing something here, because the HashMap stops working properly. First the Key becomes not unique and a different instances of Pair with same values can be found in the keySet. Also the contains key function does not work the way I suppose it to :). I clearly miss something and more likely I should somehow define a way to compare my instances from my Pair class. However i tryed implementing Comparable with compareTo in my Pair class and it still don't work. Any suggestions?

My original code is kinda messy and unfriendly to read, so i made an example just to illustrate my problem here. Here is the code:

HashMap<Pair<Integer, Integer>, StrategyPoint> myMap = new HashMap<Pair<Integer, Integer>, StrategyPoint> ();
    Pair<Integer, Integer> myPair = new Pair<Integer, Integer>(2,2);
    StrategyPoint myPoint= new StrategyPoint(2, 2, 5, 5, false);
    myMap.put(myPair, myPoint);


    Pair<Integer, Integer> searcher = new Pair<Integer, Integer> (0,0);
    searcher.setFirst(2);
    searcher.setSecond(2);
    System.out.println(myMap.containsKey(searcher));
    System.out.println(myMap.containsKey(myPair));

The result from execution is:

false

true

I have debug it and the searcher instance is beeing populated properly, however it seems the HashMap refuse to find it in its KeySet.

Thanks for the help guys!.

解决方案

You must implement properly equals and hashCode on the Pair class.

The HashMap uses these methods to differentiate and hash the key class.

这篇关于如何使自定义密钥类型使Java HashMap正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆