HashSet<String>, HashMap<String, String>, Hashtable<String, String>
来源:互联网 发布:ios 阿里百川的云推送 编辑:程序博客网 时间:2024/05/20 23:32
在java的集合中,判断两个对象是否相等的规则是:
1. 判断hashCode是否相等
如果不相等,认为两个对象也不相等。
如果相等,转入2(这一点只是为了提高存储效率而要求的,其实理论上没有也可以,但如果没有,实际使用时效率会大大降低,所以我们这里将其做为必需的。)
2. 判断两个对象用equals运算是否相等(内容是否相等)
如果不相等,认为两个对象也不相等
如果相等,认为两个对象相等
为什么是两条准则,难道用第一条不行吗?
不行,因为前面已经说了,hashcode()相等时,equals()方法也可能不等,所以必须用第2条准则进行限制,才能保证加入的为非重复元素。
HashSet
不允许出现重复数据,数据存储的位置也是不固定的。
package com.smarterp.filter;import java.util.HashSet;public class Test { public static void main(String[] args) { Test.runSequence(); } //测试运行顺序 public static void runSequence() { String a = new String("123"); String b = new String("123"); System.out.println("a 的哈希码" + a.hashCode()); System.out.println("b 的哈希码" + b.hashCode()); System.out.println(a.equals(b)); HashSet<String> set = new HashSet<String>(); set.add(a); set.add(b); System.out.println("set 的长度" + set.size()); }}
a 的哈希码48690b 的哈希码48690trueset 的长度1
总结:因为String, Integer 等类重写了hashCode(),a和b的哈希码都一样,内容equals也是一样的。HashSet会把a和b当做重复数据,所以他的长度只有
如果把HashSet换成HashSet
package com.smarterp.filter;import java.util.HashSet;public class Test { public static void main(String[] args) { Test.runSequence(); } //测试运行顺序 public static void runSequence() { class Student{ Student(String name, Integer age){}; } Student a = new Student("黎明", 40); Student b = new Student("黎明", 40); System.out.println("a 的哈希码" + a.hashCode()); System.out.println("b 的哈希码" + b.hashCode()); System.out.println(a.equals(b)); HashSet<Student> set = new HashSet<Student>(); set.add(a); set.add(b); System.out.println("set 的长度" + set.size()); }}
a 的哈希码752550817b 的哈希码1590384136falseset 的长度2
出现的结果不同,其原因就是Student类直接继承Object类而没有重写,hashCode()和equals方法,导致hashcode不一样。
hashset直接就把他认为这是2个不同的对象。
0 0
- HashSet<String>, HashMap<String, String>, Hashtable<String, String>
- string
- String
- String
- string
- string
- String
- string
- String
- string
- string
- string
- string
- String
- String
- string
- string +
- String
- hdu1520 Anniversary party
- 见过的最全的iOS面试题
- loaded the "ViewControll" nib but theview outlet was not set
- 使用Volley框架的ImageLoader加载网络图片
- Matlab变量及其命名规则
- HashSet<String>, HashMap<String, String>, Hashtable<String, String>
- SharePoint 的Discussion List的Discussion Content Type属于Folder 类型,并非是Item类型
- Android UI 设计——EditText 控件
- Hadoop--ZooKeeper
- 从uboot传参数给linux驱动
- Cstring中GetBuffer()方法的主要作用
- hdu1517 A Multiplication Game
- Android_07_有序广播及最终广播接收者
- linux中vi编辑器