简体   繁体   English

HashMap的get()方法

[英]get() method for HashMap

I'm trying to get a value with the key. 我正在尝试通过钥匙获得价值。 I'm using get() method. 我正在使用get()方法。 My key is an object composed with int and String. 我的关键是一个由int和String组成的对象。 So I make and object 所以我提出反对

HashMap<Keys,String> test = readFile(fileName2);
Keys myKey = new Keys(2,"SM");
test.get(myKey);

And I received null. 而且我收到了空值。 When I look at debbuging mode or when I print keySet I received something like this 当我查看调试模式或打印keySet时,会收到类似的信息

[Keys@d9c6e2, Keys@10285d8, Keys@c3dd7e]

although my key should be 虽然我的钥匙应该是

[1,"GM", 2,"SM", 3"PM"]

why the key look like this Keys@d9c6e2 instead of 2,"SM"? 为什么键看起来像这个Keys@d9c6e2而不是2,"SM"? and how to get the value with the key 2,"SM" ? 以及如何通过键2,"SM"获取值?

I override toString methid in Keys . 我在Keys中重写了toString方法。 It looks better but still i have null value and im sure there is some value. 它看起来更好,但我仍然具有空值,并且肯定有一些价值。

    Keys myKey = new Keys(2,"GL-G--T");
    System.out.println(myKey.toString());
    System.out.println(test.get(myKey.toString()));
    Set keyset = test.keySet();
    System.out.println(keyset);


    2,GL-G--T
    null
    [3,PNSN--G, 2,GL-G--T, 1,SM]

You need to override toString method on your Keys object. 您需要在Keys对象上重写toString方法。 Otherwise you will get the default toString provided by java.lang.Object . 否则,您将获得java.lang.Object提供默认toString

You could implement the toString method to look something like this: 您可以实现toString方法,如下所示:

public class Keys {
    private final Integer i;
    private final String s;

    public Keys(Integer i, String s) {
        this.i = i;
        this.s = s;
    }

    @Override 
    public String toString() {
        return i + "," + s;
    }
}

if you want the quotes to be displayed then you'd need to provide those: 如果您希望显示报价,则需要提供这些报价:

return i + ",\"" + s + "\"";

You'll also need to override the equals and hashCode for this object to be used as a key in a map: 您还需要重写equals和hashCode,以便将此对象用作映射中的键:

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Keys)) {
            return false;
        }
        Keys other = (Keys)o;
        return other.s.equals(s) && other.i.equals(i);
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

If you don't override equals and hashcode, then the map uses the default implementations, which results in two Keys objects with the same values being unequal. 如果不重写equals和hashcode,则映射将使用默认实现,这将导致两个具有相同值的Keys对象不相等。

You could as you are doing use a special Keys object as the key to your hash map-- you then just need to correctly implement hashCode and equals on that Keys class as others have explained. 可能正在使用特殊的Keys对象作为哈希映射的键-然后,您只需要正确实现hashCode并在该Keys类上实现equals即可,正如其他人所解释的那样。

Unless you have a specific reason not to, though, you really could just use a String as the key to the hash map. 但是,除非有特殊原因,否则您实际上只能使用String作为哈希映射的键。 Create some method such as the following: 创建一些方法,如下所示:

private static String getHashMapKeyFor(int intKey, String stringKey) {
  return stringKey + "|" + intKey;
}

and declare your hash map as taking a String as the key type. 并声明您的哈希映射为String作为键类型。 Then, whenever you want to put/find a value in the hash map, call the above method first to get the actual key to use to the hash map. 然后,每当您想在哈希图中放置/查找值时,请先调用上述方法以获取要用于哈希图的实际密钥。

Using the custom object class may have a superficial air of "correctness" or "engineeredness" to it, but in reality, just using a String will generally perform equally well and if anything may even save slightly on memory. 使用自定义对象类可能对它具有表面上的“正确性”或“工程性”,但是实际上,仅使用String即可表现一般,甚至可以节省一点内存。

In your Keys.java object override the toString method. 在Keys.java对象中重写toString方法。 Currently it's using the method defined in java.lang.Object#toString 当前它正在使用java.lang.Object#toString中定义的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM