哈希表
1.一个实际需求问题

2.哈希表的基本介绍

3.哈希表提出背景

在java和数据库中间设置缓存层,可以将当下需要进行频繁操作的数据放入缓存层,减小对数据库频繁访问的压力。缓存层可以设置多级
4.功能实现

5.代码
(1)head中不存储数据,只存下一节点地址
1 package HashTable; 2 /* 3 * head不存储数据,只存储下一个节点的地址 4 * 代码优化 5 */ 6 public class HashTableDemo1{ 7 public static void main(String[] args){ 8 9 Node1 n1=new Node1(1,"佟湘玉",30,"女"); 10 Node1 n2=new Node1(2,"白展堂",28,"男"); 11 Node1 n3=new Node1(3,"郭芙蓉",27,"女"); 12 Node1 n4=new Node1(4,"吕秀才",28,"男"); 13 Node1 n5=new Node1(5,"李大嘴",29,"男"); 14 Node1 n6=new Node1(6,"莫小贝",10,"女"); 15 Node1 n7=new Node1(7,"邢育森",35,"男"); 16 Node1 n8=new Node1(8,"燕小六",26,"男"); 17 18 Node1 l1=new Node1(10,"陆三金",30,"男"); 19 Node1 l2=new Node1(11,"盛秋月",32,"女"); 20 21 HashTable1 t=new HashTable1(10); 22 23 t.add(n1); 24 t.add(n2); 25 t.add(n3); 26 t.add(n4); 27 t.add(n5); 28 t.add(n6); 29 t.add(n7); 30 t.add(n8); 31 t.add(l1); 32 t.add(l2); 33 34 t.show(); 35 36 t.find(1); 37 38 t.delete(1); 39 t.show(); 40 41 42 43 } 44 } 45 46 47 class Node1{ 48 Node1 next; 49 int no; 50 String name; 51 int age; 52 String gender; 53 54 public Node1(){ 55 56 } 57 58 public Node1(int no, String name, int age, String gender) { 59 this.no= no; 60 this.name = name; 61 this.age = age; 62 this.gender = gender; 63 } 64 65 66 @Override 67 public String toString() { 68 return "编号: " + no + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + gender ; 69 } 70 71 72 73 74 } 75 76 77 class PeopleLinkedList1{ 78 Node1 head=new Node1(); 79 80 81 //增加节点 82 public void add(Node1 n){ 83 Node1 temp; 84 temp=head; 85 while(temp.next!=null){ 86 temp=temp.next; 87 } 88 temp.next=n; 89 } 90 91 92 //查询节点 93 public void find(int id,int no){ 94 if(head.next==null){ 95 System.out.println("第"+id+"号链表为空!!!"); 96 }else{ 97 Node1 temp=head.next; 98 while(true){ 99 if(temp==null){ 100 System.out.println("无此人数据"); 101 break; 102 }else{ 103 if(temp.no==no){ 104 System.out.println("第"+id+"号链表"); 105 System.out.println(temp); 106 break; 107 }else{ 108 temp=temp.next; 109 } 110 } 111 } 112 } 113 } 114 115 116 //遍历节点 117 public void show(int i){ 118 if(head.next==null){ 119 System.out.println("--------------------"); 120 System.out.println("第"+i+"条链表为空!!!"); 121 System.out.println("--------------------"); 122 }else{ 123 Node1 temp=head.next; 124 System.out.println("--------------------"); 125 System.out.println("第"+i+"条链表:"); 126 while(temp!=null){ 127 System.out.println(temp); 128 temp=temp.next; 129 } 130 System.out.println("--------------------"); 131 } 132 } 133 134 135 //删除节点 136 public void delete(int no){ 137 if(head.next==null){ 138 System.out.println("此链表为空!!!"); 139 }else{ 140 Node1 temp=head; 141 while(true){ 142 if(temp.next==null){ 143 if(temp.no==no){ 144 temp=null; 145 break; 146 }else{ 147 System.out.println("无此人,删除出错!!!"); 148 break; 149 } 150 }else{ 151 if(temp.next.no==no){ 152 temp.next=temp.next.next; 153 System.out.println("删除成功!!!"); 154 break; 155 }else{ 156 temp=temp.next; 157 } 158 } 159 } 160 } 161 } 162 163 164 165 166 167 } 168 169 170 class HashTable1{ 171 int size; 172 PeopleLinkedList1[] pk; 173 174 175 public HashTable1(int size) { 176 this.size = size; 177 pk=new PeopleLinkedList1[size]; 178 179 //实例化size个 PeopleLinkedList1对象,否则pk[i]无法使用PeopleLinkedList1类中的方法 180 for(int i=0;i<size;i++){ 181 pk[i]=new PeopleLinkedList1(); 182 } 183 184 } 185 186 187 188 //映射 189 public int Reflect(int no){ 190 return no%size; 191 } 192 193 194 //添加 195 public void add(Node1 n){ 196 int PeopleLinkedListId=Reflect(n.no); 197 pk[PeopleLinkedListId].add(n); 198 } 199 200 //遍历 201 public void show(){ 202 for(int i=0;i<size;i++){ 203 pk[i].show(i); 204 } 205 } 206 207 208 //查找 209 public void find(int no){ 210 int id=Reflect(no); 211 for(int i=0;i<size;i++){ 212 if(id==i){ 213 pk[i].find(id,no); 214 } 215 } 216 } 217 218 //删除 219 public void delete(int no){ 220 int id=Reflect(no); 221 for(int i=0;i<size;i++){ 222 if(id==i){ 223 pk[i].delete(no); 224 } 225 } 226 } 227 } 228 229
(2)head中存储数据
1 package HashTable; 2 3 4 /* 5 * head存储数据 6 * 代码优化 7 */ 8 public class HashTableDemo2{ 9 public static void main(String[] args){ 10 11 Node2 n1=new Node2(1,"佟湘玉",30,"女"); 12 Node2 n2=new Node2(2,"白展堂",28,"男"); 13 Node2 n3=new Node2(3,"郭芙蓉",27,"女"); 14 Node2 n4=new Node2(4,"吕秀才",28,"男"); 15 Node2 n5=new Node2(5,"李大嘴",29,"男"); 16 Node2 n6=new Node2(6,"莫小贝",10,"女"); 17 Node2 n7=new Node2(7,"邢育森",35,"男"); 18 Node2 n8=new Node2(8,"燕小六",26,"男"); 19 20 Node2 l1=new Node2(10,"陆三金",30,"男"); 21 Node2 l2=new Node2(11,"盛秋月",32,"女"); 22 23 HashTable2 t=new HashTable2(10); 24 25 t.add(n1); 26 t.add(n2); 27 t.add(n3); 28 t.add(n4); 29 t.add(n5); 30 t.add(n6); 31 t.add(n7); 32 t.add(n8); 33 t.add(l1); 34 t.add(l2); 35 36 t.show(); 37 38 System.out.println("***********"); 39 t.find(1); 40 System.out.println("***********"); 41 t.delete(1); 42 t.show(); 43 44 45 } 46 } 47 48 49 class Node2{ 50 Node2 next; 51 int no; 52 String name; 53 int age; 54 String gender; 55 56 public Node2(){ 57 58 } 59 60 public Node2(int no, String name, int age, String gender) { 61 this.no= no; 62 this.name = name; 63 this.age = age; 64 this.gender = gender; 65 } 66 67 68 @Override 69 public String toString() { 70 return "编号: " + no + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + gender ; 71 } 72 73 74 75 76 } 77 78 79 class PeopleLinkedList2{ 80 Node2 head=null; 81 82 83 //增加节点 84 public void add(Node2 n){ 85 if(head==null){ 86 head=n; 87 }else{ 88 Node2 temp; 89 temp=head; 90 while(temp.next!=null){ 91 temp=temp.next; 92 } 93 temp.next=n; 94 } 95 } 96 97 98 //查询节点 99 public void find(int id,int no){ 100 if(head==null){ 101 System.out.println("第"+id+"号链表为空!!!"); 102 }else{ 103 Node2 temp=head; 104 while(true){ 105 if(temp==null){ 106 System.out.println("无此人数据"); 107 break; 108 }else{ 109 if(temp.no==no){ 110 System.out.println("第"+id+"号链表"); 111 System.out.println(temp); 112 break; 113 }else{ 114 temp=temp.next; 115 } 116 } 117 } 118 } 119 } 120 121 122 //遍历节点 123 public void show(int i){ 124 if(head==null){ 125 System.out.println("--------------------"); 126 System.out.println("第"+i+"条链表为空!!!"); 127 System.out.println("--------------------"); 128 }else{ 129 Node2 temp=head; 130 System.out.println("--------------------"); 131 System.out.println("第"+i+"条链表:"); 132 while(temp!=null){ 133 System.out.println(temp); 134 temp=temp.next; 135 } 136 System.out.println("--------------------"); 137 } 138 } 139 140 141 //删除节点 142 public void delete(int no){ 143 if(head==null){ 144 System.out.println("此链表为空!!!"); 145 }else{ 146 if(head.no==no){ 147 head=head.next; 148 }else{ 149 Node2 temp=head; 150 while(true){ 151 if(temp.next==null){ 152 if(temp.no==no){ 153 temp=null; 154 break; 155 }else{ 156 System.out.println("无此人,删除出错!!!"); 157 break; 158 } 159 }else{ 160 if(temp.next.no==no){ 161 temp.next=temp.next.next; 162 System.out.println("删除成功!!!"); 163 break; 164 }else{ 165 temp=temp.next; 166 } 167 } 168 } 169 } 170 } 171 } 172 173 174 175 176 177 } 178 179 180 class HashTable2{ 181 int size; 182 PeopleLinkedList2[] pk; 183 184 185 public HashTable2(int size) { 186 this.size = size; 187 pk=new PeopleLinkedList2[size]; 188 189 //实例化size个 PeopleLinkedList1对象,否则pk[i]无法使用PeopleLinkedList1类中的方法 190 for(int i=0;i<size;i++){ 191 pk[i]=new PeopleLinkedList2(); 192 } 193 194 } 195 196 197 198 //映射 199 public int Reflect(int no){ 200 return no%size; 201 } 202 203 204 //添加 205 public void add(Node2 n){ 206 int PeopleLinkedListId=Reflect(n.no); 207 pk[PeopleLinkedListId].add(n); 208 } 209 210 //遍历 211 public void show(){ 212 for(int i=0;i<size;i++){ 213 pk[i].show(i); 214 } 215 } 216 217 218 //查找 219 public void find(int no){ 220 int id=Reflect(no); 221 for(int i=0;i<size;i++){ 222 if(id==i){ 223 pk[i].find(id,no); 224 } 225 } 226 } 227 228 //删除 229 public void delete(int no){ 230 int id=Reflect(no); 231 for(int i=0;i<size;i++){ 232 if(id==i){ 233 pk[i].delete(no); 234 } 235 } 236 } 237 } 238 239

浙公网安备 33010602011771号