具有空键和空值的HashMap [英] HashMap with Null Key and Null Value

查看:117
本文介绍了具有空键和空值的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

  import java.util。*; 

类员工{

字符串名称;

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

公共类HashMapKeyNullValue {

雇员e1;

public void display(){

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

map.put(e2,25);
System.out.println(当e2设置为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(当e1设置为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作为键和null作为值
System.out.println(在将null设置为KEY并将null设置为值时获取值);
System.out.println(e2:+ map.get(e2));
System.out.println(e1:+ map.get(e1));
System.out.println(null:+ map.get(null));

map.put(null,30);
System.out.println(仅当设置为null时才获取值);
System.out.println(e2:+ map.get(e2));
System.out.println(e1:+ map.get(e1));
System.out.println(null:+ map.get(null));


public static void main(String [] args){

new HashMapKeyNullValue()。display();


$ b $ / code>

程序的输出是:

 取值当e2设置为KEY 
e2:25
e1:25
null:25
当e1设置为KEY时获取值
e2:
e1:
null:
将null设置为KEY并将null设置为值时获取值
e2:null
e1:null
null:null
当仅设置null作为KEY时获取值
e2:30
e1:30
null:30

这里 e1,e2和null 作为密钥相互关联。所有三个都被分配到相同的哈希码?如果是,为什么?



因为所有三个看起来都不一样,所以一个值的变化会改变另一个值。这是否意味着只有一个关键项正在被制作为 HashMap e1,e2或null 如同一个键。

解决方案

HashMap null 作为关键字传递时, 不会调用哈希码,null关键字作为特例处理。

Put方法



HashMap null 键放入存储区 0 中,并将 null 映射为传递值的关键字。它通过内部使用的链表数据结构完成它。



使用的链表数据结构 HashMap

$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ > 静态类入口< K,V>实现Map.Entry< K,V> {
final K key;
V值;
条目< K,V>下一个;
final int hash;

在Entry类中, K 设置为 null ,并将值映射到在put方法中传递的值。

获取方法



Hashmap 中get方法检查key是否作为传递null 的。搜索 null 键的值 0
$ b

因此,只能有一个空键一个 hashmap 对象。

Consider the following Code :

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 the Value when setting null as KEY and null as value");
        System.out.println("e2 : "+map.get(e2));
        System.out.println("e1 : "+map.get(e1));
        System.out.println("null : "+map.get(null));

        map.put(null, "30");
        System.out.println("Getting the Value when setting only null as KEY");
        System.out.println("e2 : "+map.get(e2));
        System.out.println("e1 : "+map.get(e1));
        System.out.println("null : "+map.get(null));
    }

    public static void main(String[] args) {

        new HashMapKeyNullValue().display();

    }
}

The Output of program is :

Getting the Value When e2 is set as KEY
e2 : 25
e1 : 25
null : 25
Getting the Value when e1 is set as KEY
e2 : 
e1 : 
null : 
Getting the Value when setting null as KEY and null as value
e2 : null
e1 : null
null : null
Getting the Value when setting only null as KEY
e2 : 30
e1 : 30
null : 30

Here how e1, e2, and null as keys are related to each other. Is all three are assigned to same hashcode ? If yes, WHY ?

Since all three seems to be look different the change in one value changes the other. Does it mean that only one entry for key is being made into HashMap either e1, e2, or null beacause all treated to be like the same key.

解决方案

HashMap does not call hashcode when null is passed as key and null Key is handled as special case.

Put Method

HashMap puts null key in bucket 0 and maps null as key to passed value. it does it by linked list data structure it uses internally.

Linked list data structure used by HashMap (a static class in HashMap.java)

static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        final int hash;
}

In Entry class the K is set to null and value mapped to value passed in put method.

Get Method

While in Hashmap get method the checks if key is passed as null. Search Value for null key in bucket 0.

Hence there can only be one null key in one hashmap object.

这篇关于具有空键和空值的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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