第四篇 集合与容器
Posted zhangzhipeng001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第四篇 集合与容器相关的知识,希望对你有一定的参考价值。
package com.zzp.demo.myCollection;
/**
*
* 用于HashMap
* @author java
*
*/
public class Node2 {
int hash;
Object key;
Object value;
Node2 next;
}
package com.zzp.demo.myCollection; /** * * 自定义HashMap * @author java * */ public class HashMap01 { Node2[] table; //位桶数组 int size; //存放键值对的个数 public HashMap01() { table = new Node2[16]; //一般定义成2的整数幂 } public void put(Object key,Object value){ //定义新的节点对象 Node2 newNode = new Node2(); newNode.hash = myHash(key.hashCode(), table.length); newNode.key = key; newNode.value = value; newNode.next = null; Node2 temp = table[newNode.hash]; Node2 iterLast = null; boolean keyRepeat = false; if(temp == null){ //此处元素为空,则直接将新节点放进去 table[newNode.hash] = newNode;
size++; }else{ //此处的节点不为空,则直接遍历对应链表 while(temp != null){ //判断如果key重复,则覆盖 if(temp.key.equals(key)){ System.out.println("key重复,则覆盖"); keyRepeat = true; //只覆盖value 其他的不变 temp.value = value; break; }else{ //如果key不重复,则遍历下一个 iterLast = temp; temp = temp.next; } } if(!keyRepeat){ iterLast.next = newNode;
size++; } } } public static int myHash(int v,int length){ // System.out.println(v&(length-1));//直接位运算效率高 // System.out.println(v%(length-1));//取模效率低 return v&(length-1); } public static void main(String[] args) { HashMap01 hs = new HashMap01(); hs.put(10, "aa"); hs.put(20, "bb"); hs.put(30, "cc"); hs.put(20, "ssss"); hs.put(53, "dd"); hs.put(69, "ee"); hs.put(85, "ff"); /*for(int i = 10;i<100;i++){ System.out.println(i+"---"+myHash(i,16));//21,37,53 }*/ System.out.println(hs); } }
第二版
package com.zzp.demo.myCollection; /** * * 实现toString方法,方便查看map中的键值对信息 * @author java * */ public class HashMap02 { Node2[] table; //位桶数组 int size; //存放键值对的个数 public HashMap02() { table = new Node2[16]; //一般定义成2的整数幂 } public void put(Object key,Object value){ //定义新的节点对象 Node2 newNode = new Node2(); newNode.hash = myHash(key.hashCode(), table.length); newNode.key = key; newNode.value = value; newNode.next = null; Node2 temp = table[newNode.hash]; Node2 iterLast = null; boolean keyRepeat = false; if(temp == null){ //此处元素为空,则直接将新节点放进去 table[newNode.hash] = newNode;
size++; }else{ //此处的节点不为空,则直接遍历对应链表 while(temp != null){ //判断如果key重复,则覆盖 if(temp.key.equals(key)){ System.out.println("key重复,则覆盖"); keyRepeat = true; //只覆盖value 其他的不变 temp.value = value; break; }else{ //如果key不重复,则遍历下一个 iterLast = temp; temp = temp.next; } } if(!keyRepeat){ iterLast.next = newNode;
size++; } } } public static int myHash(int v,int length){ // System.out.println(v&(length-1));//直接位运算效率高 // System.out.println(v%(length-1));//取模效率低 return v&(length-1); } @Override public String toString() { StringBuilder sb = new StringBuilder("{"); for(int i=0;i<table.length;i++){ Node2 temp = table[i]; while(temp != null){ sb.append(temp.key+":"+temp.value+","); temp = temp.next; } } sb.setCharAt(sb.length()-1, ‘}‘); return sb.toString(); } public static void main(String[] args) { HashMap02 hs = new HashMap02(); hs.put(10, "aa"); hs.put(20, "bb"); hs.put(30, "cc"); hs.put(20, "ssss"); hs.put(53, "dd"); hs.put(69, "ee"); hs.put(85, "ff"); /*for(int i = 10;i<100;i++){ System.out.println(i+"---"+myHash(i,16));//21,37,53 }*/ System.out.println(hs.toString()); } }
第三版
package com.zzp.demo.myCollection; /** * * 根据键对象获取值对象 * @author java * */ public class HashMap03 { Node2[] table; //位桶数组 int size; //存放键值对的个数 public HashMap03() { table = new Node2[16]; //一般定义成2的整数幂 } public Object get(Object key){ int hash = myHash(key.hashCode(), table.length); Object value = null; if(table[hash] != null){ Node2 temp = table[hash]; while(temp != null){ if(temp.key.equals(key)){ value = temp.value; break; }else{ temp = temp.next; } } } return value; } public void put(Object key,Object value){ //定义新的节点对象 Node2 newNode = new Node2(); newNode.hash = myHash(key.hashCode(), table.length); newNode.key = key; newNode.value = value; newNode.next = null; Node2 temp = table[newNode.hash]; Node2 iterLast = null; boolean keyRepeat = false; if(temp == null){ //此处元素为空,则直接将新节点放进去 table[newNode.hash] = newNode;
size++; }else{ //此处的节点不为空,则直接遍历对应链表 while(temp != null){ //判断如果key重复,则覆盖 if(temp.key.equals(key)){ System.out.println("key重复,则覆盖"); keyRepeat = true; //只覆盖value 其他的不变 temp.value = value; break; }else{ //如果key不重复,则遍历下一个 iterLast = temp; temp = temp.next; } } if(!keyRepeat){ iterLast.next = newNode;
size++; } } } public static int myHash(int v,int length){ // System.out.println(v&(length-1));//直接位运算效率高 // System.out.println(v%(length-1));//取模效率低 return v&(length-1); } @Override public String toString() { StringBuilder sb = new StringBuilder("{"); for(int i=0;i<table.length;i++){ Node2 temp = table[i]; while(temp != null){ sb.append(temp.key+":"+temp.value+","); temp = temp.next; } } sb.setCharAt(sb.length()-1, ‘}‘); return sb.toString(); } public static void main(String[] args) { HashMap03 hs = new HashMap03(); hs.put(10, "aa"); hs.put(20, "bb"); hs.put(30, "cc"); hs.put(20, "ssss"); hs.put(53, "dd"); hs.put(69, "ee"); hs.put(85, "ff"); /*for(int i = 10;i<100;i++){ System.out.println(i+"---"+myHash(i,16));//21,37,53 }*/ System.out.println(hs.toString()); System.out.println(hs.get(85)); } }
第四版
package com.zzp.demo.myCollection; /** * * 用于HashMap * @author java * */ public class Node3<K,V> { int hash; K key; V value; Node3 next; }
package com.zzp.demo.myCollection; /** * * 增加泛型 * @author java * */ public class HashMap04<K,V> { Node3[] table; //位桶数组 int size; //存放键值对的个数 public HashMap04() { table = new Node3[16]; //一般定义成2的整数幂 } public V get(K key){ int hash = myHash(key.hashCode(), table.length); V value = null; if(table[hash] != null){ Node3 temp = table[hash]; while(temp != null){ if(temp.key.equals(key)){ value = (V)temp.value; break; }else{ temp = temp.next; } } } return value; } public void put(K key,V value){ //定义新的节点对象 Node3 newNode = new Node3(); newNode.hash = myHash(key.hashCode(), table.length); newNode.key = key; newNode.value = value; newNode.next = null; Node3 temp = table[newNode.hash]; Node3 iterLast = null; boolean keyRepeat = false; if(temp == null){ //此处元素为空,则直接将新节点放进去 table[newNode.hash] = newNode; size++; }else{ //此处的节点不为空,则直接遍历对应链表 while(temp != null){ //判断如果key重复,则覆盖 if(temp.key.equals(key)){ System.out.println("key重复,则覆盖"); keyRepeat = true; //只覆盖value 其他的不变 temp.value = value; break; }else{ //如果key不重复,则遍历下一个 iterLast = temp; temp = temp.next; } } if(!keyRepeat){ iterLast.next = newNode; size++; } } } public static int myHash(int v,int length){ // System.out.println(v&(length-1));//直接位运算效率高 // System.out.println(v%(length-1));//取模效率低 return v&(length-1); } @Override public String toString() { StringBuilder sb = new StringBuilder("{"); for(int i=0;i<table.length;i++){ Node3 temp = table[i]; while(temp != null){ sb.append(temp.key+":"+temp.value+","); temp = temp.next; } } sb.setCharAt(sb.length()-1, ‘}‘); return sb.toString(); } public static void main(String[] args) { HashMap04<Integer,String> hs = new HashMap04<>(); hs.put(10, "aa"); hs.put(20, "bb"); hs.put(30, "cc"); hs.put(20, "ssss"); hs.put(53, "dd"); hs.put(69, "ee"); hs.put(85, "ff"); /*for(int i = 10;i<100;i++){ System.out.println(i+"---"+myHash(i,16));//21,37,53 }*/ System.out.println(hs.toString()); System.out.println(hs.get(85)); } }
以上是关于第四篇 集合与容器的主要内容,如果未能解决你的问题,请参考以下文章
深入理解DOM节点类型第四篇——文档片段节点DocumentFragment