- Equals() & Hashcode()方法
- ADT的定义及单向链表的常见操作(addToFront()、addToLast()、removeAtIdx()、size()、clear())
- 泛型
Boolean equals(Object obj)
- reflexive: if X is not null, then X.equals(X) has to be true
- symmetric: if X, Y not null ,then both X.equals(Y) is true IF AND ONLY IF Y.equals(X) is true
- transitive, if X,Y,Z not null, then if X.equals(Y) is true AND Y.equals(Z) is true, X.equals(Z) must also be true
- consistent: if X, Y not null, then multiple invocations of X.equals(Y) consistently return true or consistently return false(if no information used in equals comparisons on the object is modified)
- if X is not null, then X.equals(null) has to return false
- the Object class equals() implementation: if X,Y not null, then X.equals(Y) is true IF AND ONLY IF X==Y is true
int hashCode()
- hash codes are used by hash table and related data structures
- the hashCode() method must consistently return the same integer(if no information used in equals comparisons on the object is modified) during application execution
- if a.equals(b) is true--> a. hashCode() has to return the same value as b.hashCode()
- if a.equals(b) is false--> a.hashCode() does NOT have to return a different value compared to b.hashCode(), but it is best if it did(leads to improved hash tables performance)
- the Obejct class hashCode() implementation returns distinct integers for distince objects( typically implemented through converting the internal address of the object into an integer):
--but it is NOT required in Java--> your overrides don't need to do it, but should
HashCode()、equals()的区别
1.hashCode()方法和equals()方法作用
1.1hashCode()和equals()都是Object类中的方法
如果类中不重写此方法
hashCode(): 属于是本地方法,返回的是对象的地址值
equals():用来比较两个对象的地址值是否相等
如果类中重写此方法
hashCode():返回的是根据对象的成员变量,计算出的一个整数
equals():比较的是两个对象中的成员信息是否相同
1.2类中重写hashCode()和equals()比较两个对象是否相等
两个对象通过equals()比较是相等的,那么hashCode()肯定相等,也就是equals()绝对是可靠的
两个对象通过hashCode()比较是相等,但是equals()去做比较不一定相等,也就是hashCode()不是绝对可靠的
1.3通过hashCode()和equals()搭配使用比较对象是否相等,是如何提高效率的
问题: 对于一个对象中有大量的成员信息,用equals比较会降低效率
解决: 先可以通过hashCode进行比较,如果不相等则两个对象一定不同,如果相同,在通过equals()进行比较,这样既可以判断对象是否相同又可以提高效率。
2.在HashSet集合中,通过hashCode()和equals(),保证元素的唯一性
HashSet保证元素唯一性的原理?
通过查看add方法的源码,我们知道了添加功能的执行过程中,是进行了数据的判断的。
这个判断流程是:
首先比较对象的哈希值是否相同,这个哈希值是根据对象的hashCode()计算出来的。
如果哈希值不同,就直接添加到集合中
如果哈希值相同,继续执行equals()进行比较
返回的是true, 说明元素重复,不添加。
返回的是false,说明元素不重复,就添加
结论:如果我们使用HashSet集合存储对象,你要想保证元素的唯一性,就必须重写hashCode和equals()方法
Abstract Data Type
Abstract Data Type is a logical description of how we view the data and the operations that are allowed without regard to how they are implemented. We are concerned only with what the data is representing and not with how it will eventually be constructed.
Note:
--This approach lends itself to encapsulation(group data/ information and relevant operations together) and information hiding, as well as and the overall object-oriented approach. Don't you think?
Abstract Data Type: List
A List(or sequence) is an Abstract Data type that represents a countable number of ordered values, where the same value may occur than once(unlike sets)
Lists are :
- a simple example of collections,
- a simple example of containers, as they can contain other values. If the same value occurs multiple times, each occurance is considered a distinct item.
The term List is also used for several concrete data structures that can be used to implement List ADT: arrays, linked lists, etc.