冲突解决线性探测Java

冲突解决线性探测Java,java,linear-probing,Java,Linear Probing,所以我尝试在我的线性方法中检测冲突,这是散列我的哈希映射studentMap的键。我有线性探测的基本功能,但是我很难检测到是否已经存在一个键(因此+1)。到目前为止,此代码不起作用-它不会检查my map studentMap中的键是否存在。 非常感谢任何帮助!我已经删除了一些其他的散列方法来减少代码的大小,因为它们是不相关的 public class Main { Student student; public static boolean vartrue; HashMa

所以我尝试在我的线性方法中检测冲突,这是散列我的哈希映射studentMap的键。我有线性探测的基本功能,但是我很难检测到是否已经存在一个键(因此+1)。到目前为止,此代码不起作用-它不会检查my map 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;
     }


}
公共类主{
学生;
公共静态布尔值vartrue;
HashMap-next;
公共HashMap-studentMap;
公共静态void main(字符串[]args)抛出NoSuchFieldException、IllegalArgumentException、IllegalAccessException、NoSuchFieldException{
HashMap studentMap=新的HashMap(16,0.75f);
//et键和值
studentMap.keySet().forEach((键)->{
字符串值=studentMap.get(键);
System.out.println(“Key=“+Key+”,Value=“+Value”);
});   
//向数组中添加值
学生地图。放置(“16012804”,“詹妮弗”);
学生地图。放置(“13747732”,“比阿特丽斯”);
学生地图。put(“14056983”,“Mavis”);
学生地图。放置(“16013464”,“杰弗里”);
学生地图.put(“14058392”,“Bob”);
学生地图。付诸表决(“15405833”,“法案”);
学生地图。put(“14058039”,“格特鲁德”);
学生地图。放置(“13056496”,“桃乐丝”);
//遍历数组
Set=studentMap.entrySet();
迭代器迭代器=set.Iterator();
while(iterator.hasNext()){
Map.Entry mapentry=(Map.Entry)迭代器.next();
System.out.print(“键为:“+mapentry.getKey()+”&值为:”);
System.out.println(mapentry.getValue());
}
//基于键获取值
字符串var=studentMap.get(“16012804”);
System.out.println(“指数1处的值为:“+var”);
//根据键删除值
学生地图。删除(“16012804”);
System.out.println(“删除后映射键和值:”);
Set set2=studentMap.entrySet();
迭代器迭代器2=set2.Iterator();
while(iterator2.hasNext()){
Map.Entry mapentry2=(Map.Entry)迭代器2.next();
System.out.print(“键为:”+mapentry2.getKey()+”&值为:”;
System.out.println(mapentry2.getValue());
}
Set keyset=studentMap.keyset();
System.out.println(“键集值为:“+键集”);
布尔值val=studentMap.isEmpty();
System.out.println(“哈希映射为空:+val”);
//获取价值
集合值=studentMap.values();
System.out.println(“映射值=”+值);
//桌子的大小
System.out.println(“哈希表的大小:+studentMap.Size());
//初始容量
系统输出打印项次(“初始容量:+16);
//地图容量
System.out.println(“映射容量:”+mapcapacity(studentMap));
//负荷系数
System.out.println(“负载系数:“+loadFactor(studentMap));
//线性探测
System.out.println(“…”);
System.out.println(“散列值(\“Jennifer\”)=“+linear(studentMap,“16012804”));
System.out.println(“散列值(\“Mavis\”)=“+linear(studentMap,“14056983”));
System.out.println(“散列值(\“Geoffrey\”)=“+linear(studentMap,“16013464”));
System.out.println(“哈希值(\“Bill\”)=“+linear(studentMap,“15405833”));
System.out.println(“散列值(\“Gertrude\”)=“+linear(studentMap,“14058039”));
System.out.println(“散列值(\“Beatrice\”)=“+linear(studentMap,“13747732”));
System.out.println(“散列值(\“Bob\”)=“+linear(studentMap,“14058392”));
if(vartrue=true)
{
Map map1=新HashMap(mapcapacity(studentMap)*2);
地图1.putAll(学生地图);
//新哈希映射的容量。(反射)
System.out.println(“映射1映射=”+map1);
Field tableField=HashMap.class.getDeclaredField(“表”);
tableField.setAccessible(true);
Object[]table=(Object[])tableField.get(map1);
System.out.println(“地图1的大小:”);
System.out.println(table==null?0:table.length);
}
}
//何时增加hashmap大小由hashmap的容量除以负载因子计算:
公共静态双加载因子(Map studentMap){
双重计数=studentMap.size();
双负载=计数/映射容量(studentMap);
返回载荷;
}
//如果映射的大小大于映射容量*负载系数-则将映射的大小加倍。
公共静态整数映射容量(Map studentMap){
//默认容量和负载系数
整数初始容量=11;
浮动荷载系数=0.75f;
boolean capacityFound=false;
整数容量=初始容量;
整数大小=studentMap.size();
而(!capacityFound){
if(尺寸>容量*负载系数){
//增容
容量=容量*2;
vartrue=true;
}
否则{
capacityFound=真;
}
}
返回能力;
}
//线性探测
公共静态int hashThis(字符串键,Map studentMap){
return key.hashCode()&0x7fffffff%mappacity(studentMap);
}
公共静态int线性(映射studentMap,字符串键){
字符串值=studentMap.get(key.toString();
int计数器=0;
int hash=hashThis(key,studentMap);
if(值!=null)
{
哈希=(哈希+1)%mapcapacity(studentMap);
计数器++;
}
否则{
返回0;
}
返回散列;
}
}

据我所知,您决定手动实现自己的哈希映射,而不是使用
java.util.HashMap<
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;
}