0

我正在创建一个哈希图,它进行线性探测以找到键的索引。如果键已经在索引中,我想增加它的值,而不是在新索引中添加一个。

例如,如果我得到字符串“五、五、五”的字数,我的输出是五个 1、五个 1、五个 1,而不是五个 3。

我认为它一定是我的 containsKey 方法,它使用 get 方法检查我的密钥是否已经在地图中。下面是我的 Hashmap.java 类。

import java.util.Hashtable;
import java.util.ArrayList;
import java.lang.Object;

public class Hashmap<K,V> implements MapSet<K,V>
{
    private Object hashMap[]; //hash table
    private int capacity; // capacity == table.length
    private int collisions; // number of collisions
    private int numItems; // number of hash table entries


    public Hashmap(int arrayCapacity){
        capacity = arrayCapacity;
        hashMap = new Object[capacity];
        collisions = 0;
        numItems = 0;

    }

    //Returns true if the map contains a key-value pair with the given key
    @SuppressWarnings({"unchecked"})
    public boolean containsKey( K key ){
        return get(key) != null;    

    }


    @SuppressWarnings({"unchecked"})
    public V put(K key, V value){

        int hash = Math.abs(key.hashCode());
        int index = hash% hashMap.length; //getting a new index for the key-value-pair
        KeyValuePair<K,V> pair = (KeyValuePair<K,V>) hashMap[index];

        while(pair != null && !pair.getKey().equals(key)){
            index = (index+1)% hashMap.length;
            pair = (KeyValuePair<K,V>)hashMap[index];
            collisions++;           
        }

        if (pair == null){
            //a null spot has been found, the key value pair will be added here.
            KeyValuePair<K,V> temp = new KeyValuePair<K,V>(key,value);
            hashMap[index] = temp;
            numItems++;
            if (numItems > hashMap.length / 2) {
                ensureCapacity();
            }
        return value;
        }
        else {
            //the key is the same as one already in the hashmap.
            //sets the value of the new key to the old key.
            V oldValue = pair.getValue();
            pair.setValue(value);
            return oldValue;
        }
    }

    @SuppressWarnings({"unchecked"})    
     public V get(K key){
        int hash = Math.abs(key.hashCode());
        int index = hash% hashMap.length;
        KeyValuePair<K,V> pair = (KeyValuePair<K,V>) hashMap[index];

        if(pair == null){
            return null;
        }

        else if(pair.getKey() == key){
            return pair.getValue();
        }
        else{
            index = (index + 1)% hashMap.length;
            int progress = 0;
                while(hashMap[index] != null){
                    progress++;
                    KeyValuePair<K,V> item = (KeyValuePair<K,V>) hashMap[index];
                    if(item.getKey().equals(key)) 
                        return item.getValue();
                    if (progress == hashMap.length) 
                        break;
                }       
            return null;
        }

     }
4

1 回答 1

0

参考这个例子

import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
   public static void main(String args[])
   {
      {
      String s = "Any text for word word count";
      String[] splitted = s.split(" ");
      Map<String, Integer> hm = new HashMap<String, Integer>();
      int x;

      for (int i=0; i<splitted.length ; i++) {
         if (hm.containsKey(splitter[i])) {
            int cont = hm.get(splitter[i]);
            hm.put(splitter[i], cont + 1)
         } else {
            hm.put(splitted[i], 1);
         }
      }
   }
}
于 2018-12-06T09:36:18.547 回答