相关疑难解决方法(0)

具有空键和空值的HashMap

请考虑以下代码:

import java.util.*;

class Employee {

    String name;

    public Employee(String nm) {
        this.name=nm;
    }
}

public class HashMapKeyNullValue {

    Employee e1;

    public void display(){

        Employee e2=null;
        Map map=new HashMap();

        map.put(e2, "25");
        System.out.println("Getting the Value When e2 is set as KEY");
        System.out.println("e2 : "+map.get(e2));
        System.out.println("e1 : "+map.get(e1));
        System.out.println("null : "+map.get(null));

        map.put(e1, "");
        System.out.println("Getting the Value when e1 is set as KEY");
        System.out.println("e2 : "+map.get(e2));
        System.out.println("e1 : "+map.get(e1));
        System.out.println("null : "+map.get(null));

        map.put(null, null);   // null as key and null as value
        System.out.println("Getting …
Run Code Online (Sandbox Code Playgroud)

java collections null hashmap hashcode

16
推荐指数
2
解决办法
6万
查看次数

HashMap中的NULL键的Hashcode

我刚刚阅读了Java中HashMap和HashTable类之间的区别.在那里,我发现前者允许空键以及后来不具有相同权限的区别.就HashMap的工作而言,我知道,它在key上调用hashcode方法来查找要放置该键值对的存储区.这是我的问题:如何计算空值的哈希码或是否有空键的哈希码的默认值(如果是这样请指定值)?

java collections hashmap

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

collections ×2

hashmap ×2

java ×2

hashcode ×1

null ×1