Advertisement
fghjtr

Untitled

Nov 21st, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. public static void hashMapInsight(HashMap<String, Integer> hashMap) {
  2.     Class clsHashMap = hashMap.getClass();
  3.     try {
  4.         Field table = clsHashMap.getDeclaredField("table");
  5.         table.setAccessible(true);
  6.         Object[] buckets = (Object[]) table.get(hashMap);
  7.         // System.out.println(Arrays.toString(bucket));
  8.         Class Node = Class.forName("java.util.HashMap$Node");
  9.         int maxLength = 0;
  10.         StringBuilder longest = new StringBuilder();
  11.         for (Object bucketNode : buckets) {
  12.             if (bucketNode != null) {
  13.                 int i = 1;
  14.                 StringBuilder tmp = new StringBuilder();
  15.                 Field keyString = Node.getDeclaredField("key");
  16.                 keyString.setAccessible(true);
  17.                 tmp.append(keyString.get(bucketNode));
  18.                 tmp.append(" -> ");
  19.                 Field next = Node.getDeclaredField("next");
  20.                 next.setAccessible(true);
  21.                 while ((bucketNode = next.get(bucketNode)) != null) {
  22.                     i++;
  23.                     tmp.append(keyString.get(bucketNode));
  24.                     tmp.append(" -> ");
  25.                 }
  26.                 if (i > maxLength) {
  27.                     maxLength = i;
  28.                     longest = tmp;
  29.                 }
  30.             }
  31.         }
  32.         longest.delete(longest.length() - 4, longest.length());
  33.         System.out.println("hashmap table size is: " + buckets.length);
  34.         System.out.println("longest linkedlist is: " + longest.toString());
  35.         System.out.println("Max length of hashmap bucket is: " + maxLength);
  36.     } catch (NoSuchFieldException e) {
  37.         e.printStackTrace();
  38.     } catch (SecurityException e) {
  39.         e.printStackTrace();
  40.     } catch (IllegalAccessException e) {
  41.         e.printStackTrace();
  42.     } catch (ClassNotFoundException e) {
  43.         e.printStackTrace();
  44.     }
  45. }
  46.  
  47. public static void main(String[] args) {
  48.     int m;
  49.  //   SeparateChainingHashST<String, Integer> map;
  50.     HashMap<String, Integer> hashMap;
  51.     for (int i = 4; i < 20; i++) {
  52.         m = 1 << i;
  53.         hashMap = new HashMap<>(m);
  54. //        map = new SeparateChainingHashST<>(m);
  55.         for (int j = 0; j < (int) (m * 0.75); j++) {
  56.             String t = Integer.toString((int) (Math.random() * (m * 0.75)));
  57.             // t = Integer.toString(j);
  58. //            map.put(t, 0);
  59.             hashMap.put(t, 0);
  60.         }
  61. //        System.out.println("-------------my hashMap-------------");
  62. //        map.bucketStat();
  63.         System.out.println("------------java hashMap------------");
  64.         hashMapInsight(hashMap);
  65.         System.out.println();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement