java.util.HashMap中的key为对象的问题,对象key获得相应的value值
来源:互联网 发布:淘宝买宠物狗 编辑:程序博客网 时间:2024/05/21 17:43
java.util.HashMap中的key为对象的问题,对象key获得相应的value值
重写对象key的equals和hashCode的方法
package com;import java.util.HashMap;import java.util.Map;public class AMain { public static void main(String[] args) { Map<User, Integer> map = new HashMap<>(); String name = "aa"; // String name1 = "aa"; // System.out.println(name.hashCode()); // System.out.println(name1.hashCode()); Integer age = 123; Integer result = null; User u1 = new User(); u1.setName(name); u1.setAge(age); map.put(u1, 1000); result = map.get(u1); System.out.println(result); User u2 = new User(); u2.setName(name); u2.setAge(age); result = map.get(u2); System.out.println(result); result = map.get(u1); System.out.println(result); }}
package com;public class User { private String name; private Integer age; @Override public boolean equals(Object obj) { boolean result = false; if (this == obj) result = true; if (obj == null || getClass() != obj.getClass()) result = false; User user = (User) obj; if (user.getName() == null || user.getAge() == null) result = false; if (user.getName().equals(name) && user.getAge().equals(age)) result = true; System.out.println("equals:" + result); return result; } @Override public int hashCode() { int a = 0; if (name != null && age != null) { a = name.hashCode() + age.hashCode(); } if (name != null) { a = name.hashCode(); } if (age != null) { a = age.hashCode(); } System.out.println("hashCode:" + a); return a; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
原因:
/** * Implements Map.get and related methods * * @param hash hash for key * @param key the key * @return the node, or null if none */ final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; if ((e = first.next) != null) { if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }
HashMap在根据key获得相应的value的时候,用的hash值和equals来判断是否是同一个key
阅读全文
1 0
- java.util.HashMap中的key为对象的问题,对象key获得相应的value值
- 利用key,value形式储存对象的类(Map,HashMap)
- HashMap的key是对象
- Java 将自定义的对象作为HashMap的key
- JS获取对象的key和value
- js 获取json对象的Key、value
- js 获取json对象的Key、value
- js 获取json对象的Key、value
- js 获取map对象的key,value
- Js 怎么遍历json对象所有key及根据动态key获取值(根据k值获取相应的value值)
- HashMap中的key为什么不能为可变对象
- Java 中的HashMap<Key,Value>泛型类
- HashMap、HashTable的key和value是否可为null
- Hashtable/HashMap与key/value为null的关系
- HashMap、HashTable的key和value是否可为null
- Hashtable/HashMap与key/value为null的关系
- java对HashMap中的key或者value值进行排序!
- java对HashMap中的key或者value值进行排序!
- java 导入excle
- linux系统下的文件I/O操作
- 《PHP和MySQL WEB开发》第四章笔记
- react绑定事件
- InstantClient安装使用
- java.util.HashMap中的key为对象的问题,对象key获得相应的value值
- iOS_身份证号码校验
- 德国启用含有RFID芯片的新型身份证
- ACM单词拼接
- UVA 120Stacks of Flapjacks
- 51 nod 1574 排列转换(思维 贪心)
- Chrome开发者工具详解(1)-Elements、Console、Sources面板 Chrome开发者工具面板
- 归并排序(递归实现)
- SpringMVC4.* @ResponseBody 中文乱码无法解决