map除去空值java_java – 具有空键和空值的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 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();

}

}

程序的输出是:

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

这里e1,e2和null如何作为键相互关联.是否所有三个都分配给相同的哈希码?如果是,为什么?

由于所有三个看起来都不同,一个值的变化会改变另一个.这是否意味着只有一个密钥条目被赋予HashMap e1,e2或null,因为所有条目都被视为相同的密钥.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值