• Java学习-hashcode综合练习


    1.自定义字符串的hashcode

    2.自定义MyHashMap

     3.内容查找性能比较

    其中:IHashMap接口、Entry类如下

    1 package Collection;
    2 
    3 public interface IHashMap {
    4     public void put(String key,Object value);
    5     public Object get(String key);
    6         
    7 }
     1 package Collection;
     2 
     3 public class Entry {
     4     public Object key;
     5     public Object value;
     6     public Entry(Object key,Object value){    //构造方法
     7         this.key=key;
     8         this.value=value;
     9     }
    10     public String toString(){
    11         return "[key=" + key + ", value=" + value + "]";
    12     }
    13     
    14 }

    给出代码:

      1 package Collection;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashSet;
      5 import java.util.Iterator;
      6 import java.util.LinkedList;
      7 import java.util.List;
      8 
      9 import charactor.Hero;
     10 
     11 public class myHashMap implements IHashMap {
     12     public static int hashcode(String str) {
     13         if (str.length() == 0)
     14             return 0;
     15         char tmpCharArr[] = str.toCharArray();
     16         int cnt = 0;
     17         for (int i = 0; i < tmpCharArr.length; i++)
     18             cnt += (int) tmpCharArr[i];
     19         cnt = cnt * 23;
     20         if (cnt < 0)
     21             cnt = cnt * (-1);
     22         if (cnt > 1999)
     23             cnt = cnt % 2000;
     24         return cnt;
     25 
     26         // //测试hashcode,大小为5
     27         // if(str.length()==0)
     28         // return 0;
     29         // int cnt=Integer.parseInt(str);
     30         // if(cnt>=5)
     31         // return cnt%5;
     32         // return cnt;
     33         //
     34     }
     35 
     36     public static char ranChar() {
     37         while (true) {
     38             int n = (int) Math.round(Math.random() * 128);
     39             char c = (char) n;
     40             if (Character.isDigit(c) || Character.isLetter(c))
     41                 return c;
     42         }
     43     }
     44 
     45     public static List<String> ranArr(int num) { // 随机生成长度是2-10的不等的num个字符串
     46         List<String> arr = new ArrayList(); // 最后返回arr
     47         HashSet<String> hs = new HashSet(); // 保证随机生成的字符串互不相等
     48         int i = 0;
     49         while (i < num) {
     50             int randLength = (int) (Math.round(Math.random() * 8) + 2);// 随机生成长度是2-10
     51             String tmpStr = "";
     52             for (int j = 0; j < randLength; j++) {
     53                 tmpStr += ranChar();
     54             }
     55             if (hs.add(tmpStr) == true) // 保证随机生成的字符串互不相等
     56                 i++;
     57         }
     58         for (String s : hs) { // HashSet转成List便于以后的操作
     59             arr.add(s);
     60         }
     61         return arr;
     62     }
     63 
     64     Object[] hashArr = new Object[2000]; // 自己定义的Hash表,由2000个对象组成
     65 
     66     @Override
     67     public void put(String key, Object value) {
     68         // TODO Auto-generated method stub
     69         int index = hashcode(key);
     70         // 下面两行用于应对hashArr[index]为空时,填入LinkedList<Entry>类型
     71         LinkedList<Entry> tmpLinkedList = new LinkedList();
     72         Entry E = new Entry(key, value);
     73         tmpLinkedList.add(E);
     74 
     75         if (hashArr[index] == null) {
     76             hashArr[index] = tmpLinkedList;
     77         } else {// hashArr[index]非空,说明它本身是LinkedList<Entry>类型,直接add即可
     78             LinkedList<Entry> ll = (LinkedList<Entry>) hashArr[index];
     79             ll.add(E);
     80         }
     81     }
     82 
     83     @Override
     84     public Object get(String key) {
     85         // TODO Auto-generated method stub
     86         int index = hashcode(key);
     87         if (hashArr[index] == null)
     88             return null;
     89         else {
     90             LinkedList<Entry> tmpList = (LinkedList<Entry>) hashArr[index];
     91             for (int i = 0; i < tmpList.size(); i++) {
     92                 if (tmpList.get(i).key.equals(key)) { // 找到了对应的key--value
     93                     return tmpList.get(i).value; // 返回key对应的value
     94                 } else {
     95                     return null;
     96                 }
     97             }
     98         }
     99         return null;
    100     }
    101 
    102     public Object get2(String key) { // 用于测试myHashMap性能
    103         // TODO Auto-generated method stub
    104         int index = hashcode(key);
    105         if (hashArr[index] == null)
    106             return null;
    107         else {
    108             LinkedList<Entry> tmpList = (LinkedList<Entry>) hashArr[index];
    109             int cnt = 0;//"hero-5555"的数量
    110             //"hero-5555"以外的hero也可能hash到和"hero-5555"相同的地方,只好for循环找出来
    111             for (int i = 0; i < tmpList.size(); i++) {
    112                 if (tmpList.get(i).key.equals(key)) { // 找到了对应的key-value
    113                     cnt++;
    114                 }
    115             }
    116             return cnt;
    117         }
    118     }
    119     public static void testHashCode(){
    120         List<String> rndArr=ranArr(100);
    121         List <Integer>rndArrHashCode=new ArrayList();
    122         for(int i=0;i<100;i++){
    123             rndArrHashCode.add(hashcode(rndArr.get(i)));
    124         }
    125         System.out.println("HashCode后的值为:");
    126         System.out.println(rndArrHashCode);
    127     }
    128 
    129     public static void testEfficiency(int n) {        //n是测试规模
    130         ArrayList<Hero> heros = new ArrayList<>();
    131         myHashMap mp = new myHashMap();
    132         for (int i = 0; i < n; i++) {
    133             int rnd = (int) Math.round(Math.random() * 9000 + 1000);
    134             heros.add(new Hero("hero-" + rnd)); // 办法一
    135             mp.put("hero-" + rnd, rnd); // 办法二
    136         }
    137 
    138         // 办法一:for循环查找
    139         long st1 = System.currentTimeMillis();
    140         int cnt1 = 0; // 统计"hero-5555"个数
    141         for (int i = 0; i < n; i++) {
    142             if (heros.get(i).name.equals("hero-5555"))
    143                 cnt1++;
    144         }
    145         long et1 = System.currentTimeMillis();
    146         System.out.printf("for循环耗时:%d ms
    共找到%d个
    ", (et1 - st1), cnt1);
    147 
    148         // 办法二:myHashMap查找
    149 
    150         long st2 = System.currentTimeMillis();
    151         int cnt2 = 0; // 统计"hero-5555"个数
    152         cnt2 = (int) mp.get2("hero-5555");
    153         long et2 = System.currentTimeMillis();
    154         System.out.printf("myHashMap耗时:%d ms
    共找到%d个", (et2 - st2), cnt2);
    155     }
    156     
    157     public static void main(String[] args) {
    158         testHashCode();
    159         testEfficiency(300000);
    160     }
    161 }

    效果图:

  • 相关阅读:
    WCF上传下载文件
    WCF使用相关
    .net WCF WF4.5 状态机、书签与持久化
    .net WCF WF4.5
    CSS小东西
    asp.net mvc导出execl_转载
    winform自定义控件开发
    html问题汇总
    工作中的小东西
    jQuery事件
  • 原文地址:https://www.cnblogs.com/gilgamesh-hjb/p/12229186.html
Copyright © 2020-2023  润新知