package com.wpr.collection;

import java.util.LinkedList;
import java.util.List; public class HashTable<AnyType> { private static final int DEFAULT_TABLE_SIZE = 101; private List<AnyType>[] theList;
private int curSize; public HashTable() {
this(DEFAULT_TABLE_SIZE);
} public HashTable(int size) {
//构造一个质数长度的链表
this.theList = new LinkedList[nextPrime(size)];
for(int i=0;i<theList.length;i++){
theList[i]= new LinkedList<>();
}
}
/**
* 插入,如果元素已经存在,直接返回
* @param x
*/
public void insert(AnyType x){
List<AnyType> temp = theList[myHast(x)];
if(!temp.contains(x)){
temp.add(x);
if(++curSize>theList.length)
reHash();
}
}
/**
* 表的size太小,数据的长度和表长相等时重新调整表长,装填因子为1
*/
private void reHash() {
List<AnyType>[] old = theList;
theList = new LinkedList[theList.length*2];
//更新hashTable
for(int i=0;i<theList.length;i++)
theList[i]=new LinkedList<>();
curSize = 0; for(int i=0;i<old.length;i++){
for(AnyType x:old[i])
insert(x);
}
} public void clear(){
for(List l:theList){
l.clear();
}
}
public void remove(AnyType x){
List<AnyType> temp = theList[myHast(x)];
if(temp.contains(x)){
temp.remove(x);
curSize--;
}
}
public boolean contain(AnyType x){
List<AnyType> temp = theList[myHast(x)];
return temp.contains(x);
}
/**
* 计算数据的hash值
* @param x
* @return
*/
private int myHast(AnyType x) {
int hashValue = x.hashCode(); hashValue%=theList.length;
if(hashValue<0)
hashValue+=theList.length;
return hashValue;
} /**
* 返回一个比size大的质数
* @param size
* @return
*/
private int nextPrime(int size) {
if(size%2==0)
size++;
while(!isPrime(size)){
size +=2;
}
return 0;
}
/**
* 判断size是否为质数
* @param size
* @return
*/
private boolean isPrime(int size) {
if(size%2==0||size==1)
return false;
if(size==2 || size==3)
return true;
for(int i=3;i<Math.sqrt(size);i+=2){
if(size%i==0)
return false;
}
return true;
}
}

My集合框架第四弹 HashTable(链表解决冲突)的更多相关文章

  1. Java集合框架的四个接口

    接口 [四个接口  collection  list  set  map 的区别] collection 存储不唯一的无序的数据 list 存储有序的不唯一的数据 set   存储无序的唯一的数据 m ...

  2. Java源码解析——集合框架(四)——LinkedListLinkedList原码分析

    LinkedList源码分析 LinkedList也和ArrayList一样实现了List接口,但是它执行插入和删除操作时比ArrayList更加高效,因为它是基于链表的.基于链表也决定了它在随机访问 ...

  3. Java基础知识强化之集合框架笔记67:Hashtable的实现原理

    至于Hashtable的实现原理,直接参考网友的博客,总结很全面: 深入Java集合学习系列:Hashtable的实现原理

  4. java 集合框架(十四)Queue

    一.概述 Queue一种队列结构集合,用来存储将要进行处理的元素.通常以FIFO的方式排序元素,但这并不是必须的.比如优先度队列就是一个例外,它是以元素的值来排序.但无论怎样,每个Queue的实现都必 ...

  5. My集合框架第三弹 AVL树

    旋转操作: 由于任意一个结点最多只有两个儿子,所以当高度不平衡时,只可能是以下四种情况造成的: 1. 对该结点的左儿子的左子树进行了一次插入. 2. 对该结点的左儿子的右子树进行了一次插入. 3. 对 ...

  6. Java集合框架(四)—— Queue、LinkedList、PriorityQueue

    Queue接口 Queue用于模拟了队列这种数据结构,队列通常是指“先进先出”(FIFO)的容器.队列的头部保存在队列中时间最长的元素,队列的尾部保存在队列中时间最短的元素.新元素插入(offer)到 ...

  7. 一、集合框架(HashMap和Hashtable的区别)

    一.HashMap和Hashtable 都实现了Map接口,都是以key-value形式保存数据. 区别一: HashMap可以存放null Hashtable不能存放null 区别二: HashMa ...

  8. java集合框架之HashMap和Hashtable的区别

    参考http://how2j.cn/k/collection/collection-hashmap-vs-hashtable/692.html#nowhere HashMap和Hashtable的区别 ...

  9. My集合框架第六弹 左式堆

    左式堆(Leftist Heaps)又称作最左堆.左倾堆.左式堆作为堆的一种,保留了堆的一些属性. 第1,左式堆仍然以二叉树的形式构建: 第2,左式堆的任意结点的值比其子树任意结点值均小(最小堆的特性 ...

随机推荐

  1. zipline

    history 多只股票时会返回某几只股票停牌没数据 if not symbol(stock) in data: 聚宽 多只股票如果某几只没有发行 600485: nan 多只股票如果某几只停牌 60 ...

  2. activiti参考5-任务TASK

    一.概要 1,设计TASK的表主要是:ACT_RU_TASK,ACT_HI_TASKINST(见参考-activiti表): 2,任务主要有:人工任务(usertask),服务任务(serviceta ...

  3. (转)每天一个Linux命令(6):mv

    mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...

  4. JBPM4之decision节点:2、好学生|坏学生|超级学生

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...

  5. 查一下 excel中某一列是否有重复

    另一列中写入 =IF(COUNTIF(C:C,C1)>1,"有重复","") 其余往下拖拉公式 我在想如果可以有不往下拖的呢? 不过好像筛选中也有类似的选 ...

  6. HDU 5137 How Many Maos Does the Guanxi Worth

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5120 ...

  7. a different object with the same identifier value was already associated with **(ssh异常转)

      Hibernate:a different object with the same identifier value was already associated with ...异常解决 今天 ...

  8. iOS开发-你真的会用SDWebImage?(转发)

    原文地址: http://www.jianshu.com/p/dabc0c6d083e SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高.但是你真的会用吗?本文接下来将通过例子分析 ...

  9. css 简单的 before after 笔记

    元素::first-line  段落得第一行样式 元素::first-letter 第一个字母 元素::first-before {  content:“mayufo”; } contentd的内容插 ...

  10. IdTCPServer

    IdTCPServer1 Server本身就支持多线程,一个服务端连接多个客户端. IdTCPServer1.Bindings.Add.IP := '127.0.0.1';IdTCPServer1.B ...