【问题标题】:Collision Resolution Linear Probing Java碰撞解决线性探测 Java
【发布时间】:2017-03-10 11:31:43
【问题描述】:

所以我试图检测我的线性方法中的冲突,该方法正在对我的哈希映射 studentMap 的键进行哈希处理。我有线性探测的基本功能,但是我正在努力检测一个键是否已经存在(因此 + 1)。到目前为止,此代码不起作用 - 它不会检查我的地图 studentMap 中的键是否存在。 非常感谢任何帮助!我已经删除了一些其他的哈希方法来减少这段代码的大小,因为它们是不相关的。

 public class Main  {
   Student student;
   public static boolean vartrue;
   HashMap next;
    public HashMap<String,Student> studentMap;
    public static void main(String[] args) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        HashMap<String, String> studentMap = new HashMap<>(16, 0.75f);
        //et keys and value
        studentMap.keySet().forEach((key) -> {
            String value = studentMap.get(key);
            System.out.println("Key = " + key + ", Value = " + value);
        });   
        //adding values to array
        studentMap.put("16012804", "Jennifer");
        studentMap.put("13747732", "Beatrice");
        studentMap.put("14056983", "Mavis");
        studentMap.put("16013464", "Geoffrey");
        studentMap.put("14058392", "Bob");
        studentMap.put("15405833", "Bill");
        studentMap.put("14058039", "Gertrude");
        studentMap.put("13056496", "Dorothy");
        //iterating through the array
        Set set = studentMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
            Map.Entry mapentry = (Map.Entry)iterator.next();
            System.out.print("Key is: "+ mapentry.getKey() + " & Value is: ");
            System.out.println(mapentry.getValue());
        }
       //Get values based on key
        String var= studentMap.get("16012804");
        System.out.println("Value at index 1 is: "+var);
        // Remove values based on key
        studentMap.remove("16012804");
        System.out.println("Map key and values after removal:");
        Set set2 = studentMap.entrySet();
        Iterator iterator2 = set2.iterator();
        while(iterator2.hasNext()) {
          Map.Entry mapentry2 = (Map.Entry)iterator2.next();
          System.out.print("Key is: "+mapentry2.getKey() + " & Value is: ");
          System.out.println(mapentry2.getValue());
        }
        Set keyset = studentMap.keySet();
        System.out.println("Key set values are:" + keyset);
        boolean val = studentMap.isEmpty();
        System.out.println("Is hash map empty: " + val);
        //get values
        Collection<String> values = studentMap.values();
        System.out.println("Map values = " + values);
        //size of table
        System.out.println("Size of the Hashtable: " + studentMap.size());
        //initial capacity
        System.out.println("Initial Capacity: " + 16);
        //capacity of map
        System.out.println("Map capacity: " +  mapcapacity(studentMap));
        //load factor
        System.out.println("Load Factor: " + loadFactor(studentMap));

        //linear probing
        System.out.println("...");
        System.out.println("Hash Value(\"Jennifer\")="+ linear(studentMap, "16012804"));
        System.out.println("Hash Value(\"Mavis\")="+ linear(studentMap, "14056983"));
        System.out.println("Hash Value(\"Geoffrey\")="+ linear(studentMap, "16013464"));
        System.out.println("Hash Value(\"Bill\")="+ linear(studentMap, "15405833"));
        System.out.println("Hash Value(\"Gertrude\")="+ linear(studentMap, "14058039"));
        System.out.println("Hash Value(\"Beatrice\")="+ linear(studentMap, "13747732"));
        System.out.println("Hash Value(\"Bob\")="+ linear(studentMap, "14058392"));

         if (vartrue = true)
             {
             Map<String, String> map1 = new HashMap<>(mapcapacity(studentMap) * 2);
             map1.putAll(studentMap); 
             //capacity of the new hash map. (reflection)
             System.out.println("Map 1 mappings= " + map1);
             Field tableField = HashMap.class.getDeclaredField("table");
             tableField.setAccessible(true);
             Object[] table = (Object[]) tableField.get(map1);
             System.out.println("Size of Map 1: ");
             System.out.println(table == null ? 0 : table.length);
             }

        }
    //when to increase the hashmap size is calculated by capacity of hashmap divided by load factor:
    public static double loadFactor(Map studentMap){
    double count = studentMap.size();
        double load = count/mapcapacity(studentMap);
        return load;
    }
    //if the size of the map is greater than the map capacity * load factor - then double the size of map. 
    public static Integer mapcapacity(Map studentMap){
        //default capacity and load factor
       Integer initCapacity= 11;
       float loadFactor=0.75f;
       boolean capacityFound=false;
        Integer capacity=initCapacity;
        Integer size=studentMap.size();
        while(!capacityFound){
            if(size>capacity*loadFactor){
                //increase capacity 
                capacity=capacity * 2;  
                vartrue = true; 
            }
            else {
                capacityFound=true;   
            }
        }
        return capacity;
    }




    //linear probing
    public static int hashThis(String key, Map studentMap) {
        return key.hashCode()& 0x7fffffff % mapcapacity(studentMap); 
    }
    public static int linear(Map studentMap, String key){
    String value = studentMap.get(key).toString();
    int counter = 0;
    int hash = hashThis(key, studentMap);
    if (value != null)
    {
    hash = (hash + 1) % mapcapacity(studentMap);
    counter ++;
    }
    else{
        return 0;
    }
     return hash;
     }


}

【问题讨论】:

    标签: java linear-probing


    【解决方案1】:

    据我了解,您已决定手动实现自己的哈希映射,而不是使用已经以线性探测方式运行的 java.util.HashMap 。 在这种情况下,java.util.HashMap 的来源可能是一个很大的提示。从“grepcode.com”站点,我找到java.util.HashMapput(K key, V value)方法的源代码如下;

    public V put(K key, V value) {
         ... // null check on key : omitted
    
         int hash = hash(key.hashCode());
         int i = indexFor(hash, table.length);
    
         for (Entry<K,V> e = table[i]; e != null; e = e.next) {
             Object k;
             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                 ... // if the same key already exists, return the old value : omitted
             }
         }
         modCount++;
         addEntry(hash, key, value, i);
         return null;
    }
    

    在调用addEntry() 之前,for 语句会迭代搜索可用空间。 (即退出for循环时,i表示新条目的可用空间索引。)您也可以查看get()put()的双重方法,以更好地理解。

    我认为,对您来说最重要的是java.util.HashMap 似乎不会“更改哈希码”以进行线性探测。这是与您的方法的主要区别,因为您的代码中的 linear() 似乎会在每次已经占用散列值的空间时为给定的 key 调整 hash

    此外,您的代码中的linear() 不使用迭代来搜索空闲空间,但mapcapacity() 用于大小扩展。这可能会导致单个键插入的多次空间调整,因此似乎不是线性探测的有效方法。

    综上所述,我建议查看java.util.HashMap或相关类的源代码;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多