2021-8-20学习总结

写项目,增加好友,查找好友功能,将好友使用hashtable储存

优点:

  • 数组的特点就是查找容易,插入删除困难;而链表的特点就是查找困难,但是插入删除容易。既然两者各有优缺点,那么我们就将两者的有点结合起来,让它查找容易,插入删除也会快起来。哈希表就是讲两者结合起来的产物。
  • 使用哈希函数将被查找的键转化为数组的索引。在理想的状态下,不同的键会被转化成不同的索引值。但是那是理想状态,我们实践当中是不可能一直是理想状态的。当不同的键生成了相同的索引的时候,也就是我们所说的冲突,我们这个时候就要处理冲突

Hashtable实现了Map接口,并继承Dictionary抽象类 (已过时,新的实现应该实现 Map 接口而不是扩展此类),其在JDK中的定义为:

public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable {

    /**
     * The hash table data.
     */
    private transient Entry[] table;   // 由Entry对象组成的链表数组

    /**
     * The total number of entries in the hash table.
     */
    private transient int count;   // Hashtable中Entry对象的个数

    /**
     * The table is rehashed when its size exceeds this threshold.  (The
     * value of this field is (int)(capacity * loadFactor).)
     *
     * @serial
     */
    private int threshold;   // Hashtable进行扩容的阈值

    /**
     * The load factor for the hashtable.
     *
     * @serial
     */
    private float loadFactor;   // 在其容量自动增加之前可以达到多满的一种尺度,默认为0.75

    /**
     * The number of times this Hashtable has been structurally modified
     * Structural modifications are those that change the number of entries in
     * the Hashtable or otherwise modify its internal structure (e.g.,
     * rehash).  This field is used to make iterators on Collection-views of
     * the Hashtable fail-fast.  (See ConcurrentModificationException).
     */
    private transient int modCount = 0;   // 记录Hashtable生命周期中结构性修改的次数
...
}

顺便复习下hashtable与hashmap的区别:

  • HashTable的方法前面都有synchronized来同步,是线程安全的;HashMap未经同步,是非线程安全的。
  • HashTable不允许null值(key和value都不可以) ;HashMap允许null值(key和value都可以)。
  • HashTable有一个contains(Object
    value)功能和containsValue(Object
    value)功能一样。
  • HashTable使用Enumeration进行遍历;HashMap使用Iterator进行遍历。
  • HashTable中hash数组默认大小是11,增加的方式是old*2+1;HashMap中hash数组的默认大小是16,而且一定是2的指数。
  • 哈希值的使用不同,HashTable直接使用对象的hashCode; HashMap重新计算hash值,而且用与代替求模。

写项目,思考好友分组的实现,应该也是用集合装起来

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值