Map怎么使用自定义引用类型做主键
来源:互联网 发布:wind资讯金融数据终端 编辑:程序博客网 时间:2024/06/05 06:58
问题演示
import java.util.HashMap;import java.util.Map;class Student{ private String name = "张三"; private String tel = "123";}public class TestJson { public static void main(String[] args) { Student st1 = new Student(); Student st2 = new Student(); Map<Student, String> map = new HashMap<Student, String>(); map.put(st1, null); map.put(st2, null); System.out.println(map.size()); }}
如上代码,st1,st2属性完全一样,分别使用st1,st2作为主键存入map,发现map结果等于2,为什么不是1呢?
map的存储结构有点像一个树,有很多节点,每个节点又是一个链表;
我们put一条数据时,先调用主键对象的hashcode方法找到对应的节点,
然后遍历这个节点对应的链表,使用equals方法比较是否存在和自己相同的主键;
我们调用 st1.hashcode == st2.hashcode发现是false,当然默认的equals方法取得是对象地址,肯定也是不相等,这个时候hashcode不相等,就找到不同的节点,equals方法不相等,就找不出相同的主键;
因此如果Map要是用自定义引用类型做主键,需要重写hashcode、equals方法;
class Student{ private String name = "张三"; private String tel = "123"; public int hashCode() { int result = 17; result = result * 31 + name.hashCode(); result = result * 31 + tel.hashCode(); return result; } public boolean equals(Object obj) { Student st = (Student)obj; if(null != st) { return StringUtils.equals(st.name, st.name) && StringUtils.equals(st.tel, st.tel); } return false; }}public class TestJson { public static void main(String[] args) { Student st1 = new Student(); Student st2 = new Student(); Map<Student, String> map = new HashMap<Student, String>(); map.put(st1, null); map.put(st2, null); System.out.println(map.size()); }}
阅读全文
0 0
- Map怎么使用自定义引用类型做主键
- STL MAP自定义主键
- STL map使用自定义对象做key
- c++ map 使用自定义结构做关键字
- C++中set,map使用自定义类型
- 引用类型做参数。。。
- QMap主键是自定义类型
- webService怎么返回map类型
- 使用用户自定义类型作为std::map的key
- C++ STL map中的Key使用自定义类型
- 使用用户自定义类型作为std::map的…
- 怎么使用LINQ方法来比较自定义类型对象
- JavaScript怎么做类型判断
- 使用Guid做主键和int做主键性能比较
- 使用Guid做主键和int做主键性能比较
- 使用Guid做主键和int做主键性能比较
- map 自定义类型作为键值
- 教你怎么做metro map app
- java作业
- 5、进程通信的基本概念和方法
- Web前端性能优化——网页秒加载
- 初识Nginx
- Mybatis框架基于注解的方式,实对数据现增删改查
- Map怎么使用自定义引用类型做主键
- Lintcode158 Two Strings Are Anagrams solution 题解
- 数据结构与算法分析(Java语言描述)(6)—— 归并排序(自底向上)
- [Qt]Qt5.9.0: error: GL/gl.h: 没有那个文件或目录
- leetcode18. 4Sum
- 快速排序
- 2017
- 机器学习实战 算法总结
- css样式继承体现