Java综合例题/集合框架第二版
public class Hashtable<K,V> implements Cloneable,Serializable{
/**
* 集合取空
*/
public synchronized void clear() {
Entry<?,?> tab[] = table ;
for(int index = table.length ; --index >=0 ;)
tab[index] = null ;
count = 0 ;
}
/**
* 返回原有值,如果没有就返回指定值
* @param key
* 指定的地址
* @param defaultValue
* 指定的值
* @return
* 指定值 与 原有值
*/
public synchronized V getfault(K key , V value ) {
V value1 = get(key);
return (value1==null) ? value : value1 ;
}
/**
* 如果我想知道集合是否为空 ,就用这个
* @return
* false 和 true
*
*/
public synchronized boolean isEmpty() {
return count==0 ;
}
/**
* 可以向集合添加数值 , 但不能覆盖原有数值,如果想覆盖原有数值就要调用putIfAbsent方法
* @param key
* 指定的地址号
* @param value
* 要添加的值
* @return
* 返回要添加的数值,如果指定地址存在数值,就返回原数值
*/
public synchronized V getOrDefault(K key , V value ) {
V value1 = get(key) ;
if(value1 == null)
put(key , value);
return (null==value1) ? value : value1 ;
}
/**
* 获得地址号所在的值
* @param key
* 指定的地址号
* @return
* 集合中的数值
*/
@SuppressWarnings("unchecked")
public synchronized V get(Object key) {
Entry<?,?> tab[] = table ;
int hash = key.hashCode();
int y = 0 ;
for(int g = 0 ; ; g++) {
for( ; y < tab.length ; y++ ) {
for(Entry<?,?> e = tab[y] ; e != null ; e = e.next) {
if(hash == e.hash && e.key.equals(key)) {
V value = (V)e.value ;
return value ;
}
}
if(tab[y] != null )
continue ;
}
if(g == count)
return null;
}
}
/**
* 问一下有没有这个值
* @param value
* 指定的值
* @return
* false 和 true
*/
public synchronized boolean containsValue(Object value) {
return contains(value);
}
/**
* 询问一下单位的地址号有没有
* @param key
* @return
*/
public synchronized boolean containsKey(Object key) {
Entry<?,?> tab[] = table ;
int hash = key.hashCode() ;
if(hash >= tab.length)
throw new ArrayIndexOutOfBoundsException("Cross the border : " + (tab.length-1));
int index = (hash & 0x7fffffff ) % tab.length ;
for(Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if((e.hash == hash ) && e.key.equals(key))
return true ;
}
return false ;
}
/**
* 询问一下,集合中有没有这个值
* @param value
* 指定的值
* @return
* false 和 true
*/
public synchronized boolean contains(Object value) {
if(value==null)
throw new NullPointerException("Null pointer");
Entry<?,?> tab[] = table ;
for(int i = table.length -1 ; i > 0 ; i--) {
for(Entry<?,?> e = tab[i] ; e != null ; e = e.next ) {
if(e.value.equals(value))
return true ;
}
}
return false;
}
/**
* 在空单位的地址上插入新数值 , 如果此处存在数值,就不能插入相同的值
* 否则就替换掉原来的单位
* @param key
* 单位的地址号
* @param value
* 要插入的数值
* @return
*/
@SuppressWarnings("unchecked")
public synchronized V putIfAbsent(K key , V value ) {
requireNonNull(value);
Entry<?,?> tab[] = table ;
int hash = key.hashCode() ;
for(int h = 0 ; h < tab.length ; h++ ) {
Entry<K,V> e = (Entry<K,V>)tab[h] ;
if(e != null )
for(; e != null ; e = e.next ) {
if((e.hash == hash) && e.key.equals(key) ) {
V oldValue = e.value ;
if( oldValue.equals(value)) {
return null;
}else {
e.value = value ;
}
return oldValue ;
}
}
}
put(key , value);
return null ;
}
/**
* 验证单位是否为null
* @param <T>
* @param obj
* 要验证的单位
* @return
*/
private static <T> T requireNonNull( T obj) {
if(obj == null)
throw new NullPointerException();
return obj ;
}
/**
* 替换单位
* @param key
* 单位地址号
* @param value
* 替换内容
* @return
*/
public synchronized V replace( K key , V value ) {
requireNonNull(value);
Entry<?,?> tab[] = table ;
int hash = key.hashCode();
int index = (hash & 0x7fffffff) % table.length ;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
for(; e != null; e = e.next) {
if((e.hash == hash) && e.key.equals(key)) {
V oldValue = e.value ;
e.value = value ;
return oldValue ;
}
}
return null;
}
/**
* 在集合中取出一个单位,取出后集合中再也找不到
* @param key
* 单位所在的地址号
* @return
*/
@SuppressWarnings("unchecked")
public synchronized V remove(Object key) {
Entry<?,?> tab[] = table ;
int hash = key.hashCode();
Entry<K,V> e = null;
for(int y = 0 ; y < tab.length ; y++) {
e = (Entry<K,V>)tab[y] ;
for(Entry<K,V> p = e ; e != null ; p = e , e = e.next ) {
if(e.hash == hash && e.key.equals(key)) {
if(p != null) {
p.next = e.next ;
tab[y] = e.next ;
}
count--;
V oldValue = e.value ;
e.value = null ;
return oldValue ;
}
}
}
return null;
}
/**
* 用于SerialID的JD2.0版本的互操作性
*/
private static final long serialVersionUID = 0x7fffffff;
/**
* 类里面的属性都会被复制
* @return
* 克隆类对象
*/
private final Hashtable<?,?> cloneHashtable(){
try {
return (Hashtable<?,?>)super.clone();
}catch(CloneNotSupportedException e){
throw new InternalError(e);
}
}
/**
* 返回集合参数
*/
public synchronized Object clone() {
Hashtable<?,?> hash = cloneHashtable() ;
hash.table = new Entry<?,?>[table.length];
for(int i =0 ; i < table.length;i++) {
hash.table[i] = (table[i]!=null)? (Entry<?, ?>) table[i].clone():null;
}
return hash;
}
private static final int B = 0 ;
public Hashtable() {
this(11,0.75f);
}
private static float loadFactor;
/**
* 扩展容器时需要用到
*/
private int threshold;
private static final long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
private static native int floatToRawIntBits(float value);
/**
* 返回两个{@code float}值中较小的值。也就是说,结果是更接近负无穷大的值。
* 如果参数具有相同的值,结果就是相同的值。如果任一值为NaN,则结果为NaN。
* 与数值比较运算符不同,该方法认为负零严格小于正零。
* 如果一个参数为正零,另一个参数为负零,则结果为负零。
* @param a 一个论点
* @param b 另一个论点
* @return
* {@code a}和{@code b}中较小的一个。
*//*1626*/
private static float min(float a , float b) {
// System.out.println(Float.floatToIntBits(b));
if( a != a )
return a;
if((a==0.0f) && (b == 0.0f) && (Float.floatToIntBits(b)==negativeZeroFloatBits)) {
return b ;
}
return (a<=b)?a:b;
}
private static final int JHGBJHGJG = 0x7fffffff-8;
/**
* 初始化容器
* @param integer
* 容器需要定义一个大小
* @param object
*/
protected Hashtable(int integer , float loadFactor) {
this.table = new Entry<?,?>[integer] ;
Hashtable.count = B ;
Hashtable.loadFactor = loadFactor ;
this.threshold =(int) min(integer*loadFactor,JHGBJHGJG);
this.modCount=B;
}
/**
* 实现以上代码的都要经过我,必经路径
*/
@SuppressWarnings("unchecked")
public synchronized String toString() {
int mab=size()-1;
if(mab==-1)
return "{}";
StringBuffer buff = new StringBuffer();
Entry<?,?> tab[] =table;
buff.append("{");
int a = 0 ;
for(int i = 0 ; ; i++) {
for( ; a < tab.length ; a++ ) {
Entry<K,V> value= (Entry<K,V>) tab[a];
if(value != null) {
buff.append(value.key==this ? "?" : value.key.toString());
buff.append("=");
buff.append( ((value.value==this ) ? "?" : value.value.toString()));
a++;
break;
}
}
// System.out.println(tab[3]);
if(i==mab) {
return buff.append("}").toString();
}
buff.append(" , ");
}
}
/**
* 集合里面数值的总量
* @return
*/
public synchronized int size() {
return count;
}
/**
* 帮我计算一下集合里面有多少个数值
*/
private transient static int count ;
/**
* 容器扩展
*/
private void rehash() {
int oldCapacity = table.length ;
int newCapacity = (oldCapacity<<1)+2;
Entry<?,?> oldMap[] = table;
if(newCapacity-JHGBJHGJG>0) {
if(newCapacity==JHGBJHGJG)
return;
newCapacity=JHGBJHGJG;
}
Entry<?,?>[] tab =new Entry<?,?>[newCapacity];
this.threshold = (int)min(newCapacity*loadFactor,JHGBJHGJG-8);
table = tab ;
for(int i = 0 ; i < oldCapacity ; i++) {
for(Entry<?,?> old = (Entry<?,?>)oldMap[i] ; old != null ; old = old.next ) {
Entry<?,?> e = old ;
int index = (i & 0x7fffffff) % newCapacity ;
tab[index] = e ;
}
}
}
/**
* 存值主要在这里完成
* @param hash
* 定义一个存储地址的哈希值,方便以后访问
* @param key
* 要存储的地址号
* @param value
* 要存储的值
* @param index
* 告诉我一个要存储的地址
*/
@SuppressWarnings("unchecked")
private void addEntry(int hash , K key , V value , int index) {
if(count>=threshold )
rehash();
Entry<?,?> tab[] = table;
Entry<K,V> next = (Entry<K,V>)tab[modCount];
tab[modCount++] = new Entry<>(hash , key , value , next , index);
count++ ;
}
/**
* 容器 , 被存储的数值都可以在里面找到 ,还有相关参数
* @author Administrator
*
* @param <K>
* @param <V>
*/
static class Entry<K,V>{
protected final Object clone(){
return new Entry<>(hash,key,value,next,index);
}
/**
* key的哈希值
*/
private int hash;
/**
* 要存储数值所在的地址号
*/
private K key ;
/**
* 准备存储的数值
*/
private V value;
/**
* 这是一个空参数
*/
private Entry<K,V> next;
/**
* 告诉我一个要存储数值的地址号
*/
private int index;
protected Entry(int hash , K key , V value , Entry<K,V> next , int index) {
this.hash = hash ;
this.key = key ;
this.value = value ;
this.next = next;
this.index = index ;
}
public synchronized String toString() {
return key+"="+value ;
}
}
/**
* 存储了多少次数值?
*/
private transient int modCount ;
/**
* 在存储之前我要知道,在指定的存储地址上是否存在数值?
* 如果存在我就要替换它
*/
private transient Entry<K,V> entry;
/**
* 要存储的找我
*/
private transient Entry<?,?>[] table;
/**
* 告诉我一个要存储的地址
*/
private transient int index;
/**
* 修改集合参数 ,此集合不允许存在重复值
* @param key
* 定义一个要存储数值的地址
* @param value
* 要存储的数值
* @return
* 返回一个存储在集合里面的值
*/
@SuppressWarnings("unchecked")
public synchronized V put( K key , V value) {
if(value==null)
throw new NullPointerException("Null pointer");
Entry<?,?> tab[] = table;
int hash = key.hashCode();
index = (hash & 0x7fffffff) % (hash + tab.length);
if(index < tab.length) {
Entry<K,V> entry =(Entry<K,V>) tab[index];
//把集合中的某个数值提取出来
for(;entry!=null;entry=entry.next) {//只要条件成立,就把提取出来的数值修改
if((entry.hash==hash)&&entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
}
addEntry( hash , key , value , index) ;
return value;
}
}
集合的方法调用
Hashtable<Integer,Object> hash = new Hashtable<Integer,Object>() ;
for(int o = 0 ; o < 10 ; o++) {
hash.put(o,"lkjflkjsdfjf" + o) ;
}
/*修改集合参数 ,此集合不允许存在重复值*/
System.out.println(hash.put(12,"kdjfdsjfkdjsj"));
/*在空单位的地址上插入新数值 , 如果此处存在数值,就不能插入相同的值否则就替换 掉原来的单位*/
System.out.println(hash.putIfAbsent(0, "kjshfkdhsfkhiyreq4564"));
/*询问一下,集合中有没有这个值*/
System.out.println(hash.contains("kdjfdsjfkdjsj5"));
/*问一下有没有这个值*/
System.out.println(hash.containsKey(5));
/*问一下有没有这个值*/
System.out.println(hash.containsValue("kdjfdsjfkdjsj3"));
/*获得地址号所在的值*/
System.out.println(hash.get(8));
/*返回原有值,如果没有就返回指定值*/
System.out.println(hash.getfault(7, "jfajslfj")+" "+1);
/*可以向集合添加数值 , 但不能覆盖原有数值,如果想覆盖原有数值就要调用putIfAbsent方法*/
System.out.println(hash.getOrDefault(12,"mxncjxnf23154"));
/*集合是否为空*/
System.out.println(hash.isEmpty());
/*在集合中取出一个单位,取出后集合中再也找不到*/
System.out.println(hash.remove("kdjfdsjfkdjsj9"));
/*替换单位*/
System.out.println(hash.replace(4,"ksjadfhkhf5454"));
/*集合里面数值的总量*/
System.out.println(hash.size());
/*返回集合参数*/
System.out.println(hash.clone());
/*集合直接取空*/
hash.clear();
Java能力提升必选,项目实战《集合框架》
于 2022-05-07 00:06:39 首次发布